您的位置:首页 > 理论基础 > 计算机网络

连接管理工具httpclient的简单使用

2017-04-26 15:22 274 查看
一、HttpClient简介

HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java.net 包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。HttpClient
已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。更多信息请关注http://hc.apache.org/。如果使用maven构建项目,可以引入如下依赖:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>


二、HttpClient使用示例

1、使用的基本方法

使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。

1)创建HttpClient对象。

2)创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。

3)如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。

4)调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。

5)调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。

6)释放连接。无论执行方法是否成功,都必须释放连接。

2、GET请求
package com.kang.httpclient;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class DoGET {

public static void main(String[] args) throws Exception {

// 创建Httpclient对象(相当于打开浏览器)
CloseableHttpClient httpclient = HttpClients.createDefault();

// 创建http GET请求(相当于在浏览器地址栏输入网址)
HttpGet httpGet = new HttpGet("http://www.baidu.com/");

CloseableHttpResponse response = null;
try {
// 执行请求(相当于浏览器回车发起请求)
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("内容:"+content);
}
} finally {//关闭连接,释放资源
if (response != null) {
response.close();
}
httpclient.close();
}

}

}


3、带参数的GET请求
package com.kang.httpclient;

import java.net.URI;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class DoGETParam {

public static void main(String[] args) throws Exception {

// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();

// 定义请求的参数
URI uri = new URIBuilder("https://www.baidu.com/s").setParameter("wd", "java").build();
System.out.println(uri);

// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);

CloseableHttpResponse response = null;
try {
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}

}

}


4、POST请求
package com.kang.httpclient;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class DoPOST {

public static void main(String[] args) throws Exception {

// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();

// 创建http POST请求
HttpPost httpPost = new HttpPost("http://xxxxxx");//这里发起的是post请求

CloseableHttpResponse response = null;
try {
// 执行请求
response = httpclient.execute(httpPost);
// 判断返回状态是否为200
System.out.println(response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}

}

}


5、带参数的POST请求
package com.kang.httpclient;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class DoPOSTParam {

public static void main(String[] args) throws Exception {

// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();

// 创建http POST请求
HttpPost httpPost = new HttpPost("http://xxxxxxx");

// 设置1个post参数
List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
parameters.add(new BasicNameValuePair("id", "1"));
// 构造一个form表单式的实体
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
// 将请求实体设置到httpPost对象中
httpPost.setEntity(formEntity);

CloseableHttpResponse response = null;
try {
// 执行请求
response = httpclient.execute(httpPost);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}

}

}


6、定义连接管理器
package com.kang.httpclient;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

public class HttpConnectManager {

public static void main(String[] args) throws Exception {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// 设置最大连接数
cm.setMaxTotal(200);
// 设置每个主机地址的并发数
cm.setDefaultMaxPerRoute(20);

doGet(cm);
doGet(cm);
}

public static void doGet(HttpClientConnectionManager cm) throws Exception {
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

// 创建http GET请求
HttpGet httpGet = new HttpGet("http://www.baidu.com/");

CloseableHttpResponse response = null;
try {
// 执行请求
response = httpClient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("内容长度:" + content.length());
}
} finally {
if (response != null) {
response.close();
}
// 此处不能关闭httpClient,如果关闭httpClient,连接池也会销毁
// httpClient.close();
}
}

}


7、设置请求配置信息
package com.kang.httpclient;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class RequestConfigDemo {

public static void main(String[] args) throws Exception {

// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();

// 创建http GET请求
HttpGet httpGet = new HttpGet("http://www.baidu.com/");

// 构建请求配置信息
RequestConfig config = RequestConfig.custom().setConnectTimeout(1000) // 创建连接的最长时间
.setConnectionRequestTimeout(500) // 从连接池中获取到连接的最长时间
.setSocketTimeout(10 * 1000) // 数据传输的最长时间
.setStaleConnectionCheckEnabled(true) // 提交请求前测试连接是否可用
.build();
// 设置请求配置信息
httpGet.setConfig(config);

CloseableHttpResponse response = null;
try {
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}

}

}


8、设置自动关闭无效连接
package com.kang.httpclient;

import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

public class ClientEvictExpiredConnections {

public static void main(String[] args) throws Exception {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// 设置最大连接数
cm.setMaxTotal(200);
// 设置每个主机地址的并发数
cm.setDefaultMaxPerRoute(20);

new IdleConnectionEvictor(cm).start();
}

public static class IdleConnectionEvictor extends Thread {

private final HttpClientConnectionManager connMgr;

private volatile boolean shutdown;

public IdleConnectionEvictor(HttpClientConnectionManager connMgr) {
this.connMgr = connMgr;
}

@Override
public void run() {
try {
while (!shutdown) {
synchronized (this) {
wait(5000);
// 关闭失效的连接
connMgr.closeExpiredConnections();
}
}
} catch (InterruptedException ex) {
// 结束
}
}

//调用该方法可以停止上面的线程
public void shutdown() {
shutdown = true;
synchronized (this) {
notifyAll();
}
}
}

}


三、spring整合httpclient

1、新建一个外部属性文件用于设置httpclient的配置参数

httpclient.properties内容如下:
http.maxTotal=200
http.defaultMaxPerRoute=50

http.connectTimeout=1000
http.connectionRequestTimeout=500
http.socketTimeout=10000
http.staleConnectionCheckEnabled=true


2、在Spring的配置文件中加载该属性文件
<property name="locations">
<list>
<value>classpath:httpclient.properties</value>
</list>
</property>


3、编写一个定期关闭无效连接的方法
package com.kang.httpclient;

import org.apache.http.conn.HttpClientConnectionManager;

public class IdleConnectionEvictor extends Thread {

private final HttpClientConnectionManager connMgr;

private volatile boolean shutdown;

public IdleConnectionEvictor(HttpClientConnectionManager connMgr) {
this.connMgr = connMgr;
this.start();// 启动当前线程
}

@Override
public void run() {
try {
while (!shutdown) {
synchronized (this) {
wait(5000);
// 关闭失效的连接
connMgr.closeExpiredConnections();
}
}
} catch (InterruptedException ex) {
// 结束
}
}

public void shutdown() {
shutdown = true;
synchronized (this) {
notifyAll();
}
}
}
4、编写spring整合httpclient的配置文件
applicationContext-httpclient.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 
<!-- 定义连接管理器 -->
<bean id="httpClientConnectionManager"
class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
<property name="maxTotal" value="${http.maxTotal}" />
<property name="defaultMaxPerRoute" value="${http.defaultMaxPerRoute}" />
</bean>

<!-- httpclient的构建器 -->
<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder">
<property name="connectionManager" ref="httpClientConnectionManager" />
</bean>

<!-- 定义Httpclient对象 -->
<!-- 该对象是多例的 -->
<bean class="org.apache.http.impl.client.CloseableHttpClient"
factory-bean="httpClientBuilder" factory-method="build" scope="prototype">
</bean>

<!-- 请求参数的构建器 -->
<bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig.Builder">
<!-- 创建连接的最长时间 -->
<property name="connectTimeout" value="${http.connectTimeout}" />
<!-- 从连接池中获取到连接的最长时间 -->
<property name="connectionRequestTimeout" value="${http.connectionRequestTimeout}" />
<!-- 数据传输的最长时间 -->
<property name="socketTimeout" value="${http.socketTimeout}" />
<!-- 提交请求前测试连接是否可用 -->
<property name="staleConnectionCheckEnabled" value="${http.staleConnectionCheckEnabled}" />
</bean>

<!-- 定义请求参数对象 -->
<bean class="org.apache.http.client.config.RequestConfig"
factory-bean="requestConfigBuilder" factory-method="build" />

<!-- 定期关闭无效连接 -->
<bean class="com.kang.httpclient.IdleConnectionEvictor">
<constructor-arg index="0" ref="httpClientConnectionManager"></constructor-arg>
</bean>

</beans>


四、模拟登录开心网

很多网站的内容都只是对注册用户可见的,这种情况下就必须要求使用正确的用户名和口令登录成功后,方可浏览到想要的页面。因为HTTP协议是无状态的,也就是连接的有效期只限于当前请求,请求内容结束后连接就关闭了。在这种情况下为了保存用户的登录信息必须使用到Cookie机制。以JSP/Servlet为例,当浏览器请求一个JSP或者是Servlet的页面时,应用服务器会返回一个参数,名为jsessionid(因不同应用服务器而异),值是一个较长的唯一字符串的Cookie,这个字符串值也就是当前访问该站点的会话标识。浏览器在每访问该站点的其他页面时候都要带上jsessionid这样的Cookie信息,应用服务器根据读取这个会话标识来获取对应的会话信息。

对于需要用户登录的网站,一般在用户登录成功后会将用户资料保存在服务器的会话中,这样当访问到其他的页面时候,应用服务器根据浏览器送上的Cookie中读取当前请求对应的会话标识以获得对应的会话信息,然后就可以判断用户资料是否存在于会话信息中,如果存在则允许访问页面,否则跳转到登录页面中要求用户输入帐号和口令进行登录。这就是一般使用JSP开发网站在处理用户登录的比较通用的方法。

这样一来,对于HTTP的客户端来讲,如果要访问一个受保护的页面时就必须模拟浏览器所做的工作,首先就是请求登录页面,然后读取Cookie值;再次请求登录页面并加入登录页所需的每个参数;最后就是请求最终所需的页面。当然在除第一次请求外其他的请求都需要附带上Cookie信息以便服务器能判断当前请求是否已经通过验证。说了这么多,可是如果你使用httpclient的话,你甚至连一行代码都无需增加,你只需要先传递登录信息执行登录过程,然后直接访问想要的页面,跟访问一个普通的页面没有任何区别,因为类HttpClient已经帮你做了所有该做的事情了。下面的例子实现了模拟登陆开心网并向自己好友发送消息的功能。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;

class Login {
public static String loginurl = "https://security.kaixin001.com/login/login_post.php";
static Cookie[] cookies = {};

static HttpClient httpClient = new HttpClient();

static String email = "xxx@qq.com";//你的email
static String psw = "xxx";//你的密码
// 消息发送的action
String url = "http://www.kaixin001.com/home/";

public static void getUrlContent()
throws Exception {

HttpClientParams httparams = new HttpClientParams();
httparams.setSoTimeout(30000);
httpClient.setParams(httparams);

httpClient.getHostConfiguration().setHost("www.kaixin001.com", 80);

httpClient.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");

PostMethod login = new PostMethod(loginurl);
login.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8");

NameValuePair Email = new NameValuePair("loginemail", email);// 邮箱
NameValuePair password = new NameValuePair("password", psw);// 密码
// NameValuePair code = new NameValuePair( "code"
// ,"????");//有时候需要验证码,暂时未解决

NameValuePair[] data = { Email, password };
login.setRequestBody(data);

httpClient.executeMethod(login);
int statuscode = login.getStatusCode();
System.out.println(statuscode + "-----------");
String result = login.getResponseBodyAsString();
System.out.println(result+"++++++++++++");

cookies = httpClient.getState().getCookies();
System.out.println("==========Cookies============");
int i = 0;
for (Cookie c : cookies) {
System.out.println(++i + ":   " + c);
}
httpClient.getState().addCookies(cookies);

// 当state为301或者302说明登陆页面跳转了,登陆成功了
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
|| (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
|| (statuscode == HttpStatus.SC_SEE_OTHER)
|| (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
// 读取新的 URL 地址
Header header = login.getResponseHeader("location");
// 释放连接
login.releaseConnection();
System.out.println("获取到跳转header>>>" + header);
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals("")))
newuri = "/";
GetMethod redirect = new GetMethod(newuri);
// ////////////
redirect.setRequestHeader("Cookie", cookies.toString());
httpClient.executeMethod(redirect);
System.out.println("Redirect:"
+ redirect.getStatusLine().toString());
redirect.releaseConnection();

} else
System.out.println("Invalid redirect");
} else {
// 用户名和密码没有被提交,当登陆多次后需要验证码的时候会出现这种未提交情况
System.out.println("用户没登陆");
System.exit(1);
}

}

public static void sendMsg() throws Exception {
// 登录后发消息
System.out.println("*************发消息***********");

String posturl = "http://www.kaixin001.com/msg/post.php";
PostMethod poster = new PostMethod(posturl);

poster.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8");
poster.setRequestHeader("Cookie", cookies.toString());

NameValuePair uids = new NameValuePair("uids", "89600585");// 发送的好友对象的id,此处换成你的好友id
NameValuePair content = new NameValuePair("content", "你好啊!");// 需要发送的信息的内容
NameValuePair liteeditor_0 = new NameValuePair("liteeditor_0", "你好啊!");// 需要发送的信息的内容
NameValuePair texttype = new NameValuePair("texttype", "plain");
NameValuePair send_separate = new NameValuePair("send_separate", "0");
NameValuePair service = new NameValuePair("service", "0");
NameValuePair[] msg = { uids, content, texttype, send_separate, service,liteeditor_0 };

poster.setRequestBody(msg);
httpClient.executeMethod(poster);

String result = poster.getResponseBodyAsString();
System.out.println(result+"++++++++++++");
//System.out.println(StreamOut(result, "iso8859-1"));
int statuscode = poster.getStatusCode();
System.out.println(statuscode + "-----------");
if(statuscode == 301 || statuscode == 302){
// 读取新的 URL 地址
Header header = poster.getResponseHeader("location");
System.out.println("获取到跳转header>>>" + header);
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals("")))
newuri = "/";
GetMethod redirect = new GetMethod(newuri);
// ////////////
redirect.setRequestHeader("Cookie", cookies.toString());
httpClient.executeMethod(redirect);
System.out.println("Redirect:"
+ redirect.getStatusLine().toString());
redirect.releaseConnection();

} else
System.out.println("Invalid redirect");
}

poster.releaseConnection();
}

public static String StreamOut(InputStream txtis, String code)
throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(txtis,
code));
String tempbf;
StringBuffer html = new StringBuffer(100);
while ((tempbf = br.readLine()) != null) {
html.append(tempbf + "\n");
}
return html.toString();

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: