本文整合基于httpclient-4.5.2版本。
加入httpclient依赖
<dependency>
<groupId>org.apache.httpcomponents
</groupId>
<artifactId>httpclient
</artifactId>
<version>4.5.2
</version>
</dependency>
编写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="poolingHttpClientConnectionManager"
class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
<property name="maxTotal" value="${http.maxTotal}" />
<property name="defaultMaxPerRoute" value="${http.defaultMaxPerRoute}" />
</bean>
<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder">
<property name="connectionManager" ref="poolingHttpClientConnectionManager" />
</bean>
<bean class="org.apache.http.impl.client.CloseableHttpClient"
factory-bean="httpClientBuilder" factory-method="build" scope="prototype" />
<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.taotao.web.httpclient.IdleConnectionEvictor">
<constructor-arg index="0" ref="poolingHttpClientConnectionManager"/>
</bean>
</beans>
httpclient.properties的配置文件
http.maxTotal=
200
http.defaultMaxPerRoute=
50
http.connectTimeout=
1000
http.connectionRequestTimeout=
500
http.socketTimeout=
10000
http.staleConnectionCheckEnabled=
true
httpclient封装的工具类
package com.taotao.web.service;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.taotao.web.httpclient.HttpResult;
@Service
public class ApiService implements BeanFactoryAware {
@Autowired
private RequestConfig requestConfig;
private BeanFactory beanFactory;
/**
* 解决单例对象中使用多例对象的问题
* 如:该类ApiService使用@Service注解默认是单例模式,所有引用对象httpclient只实例化一次,
* 而httpclient需要多例,可参考上面Spring配置中scope="prototype",
* 利用bean工厂手动获取httpclient实例,保证每次都是不同实例。
*/
@Override
public void setBeanFactory(BeanFactory beanFactory)
throws BeansException {
this.beanFactory = beanFactory;
}
private CloseableHttpClient
getHttpClient() {
return this.beanFactory.getBean(CloseableHttpClient.class);
}
/**
* GET请求
*
* @param url
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public String
doGet(String url)
throws ClientProtocolException, IOException {
HttpGet httpGet =
new HttpGet(url);
httpGet.setConfig(
this.requestConfig);
CloseableHttpResponse response =
null;
try {
response = getHttpClient().execute(httpGet);
if (response.getStatusLine().getStatusCode() ==
200) {
return EntityUtils.toString(response.getEntity(),
"UTF-8");
}
}
finally {
if (response !=
null) {
response.close();
}
}
return null;
}
/**
* GET 请求,带参数
*
* @param url
* @param params
* @return
* @throws ClientProtocolException
* @throws IOException
* @throws URISyntaxException
*/
public String
doGet(String url, Map<String, String> params)
throws ClientProtocolException, IOException, URISyntaxException {
if (
null == params)
return this.doGet(url);
URIBuilder uriBuilder =
new URIBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
uriBuilder.addParameter(entry.getKey(), entry.getValue());
}
return this.doGet(uriBuilder.build().toString());
}
/**
* POST请求
*
* @param url
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public HttpResult
doPost(String url)
throws ClientProtocolException, IOException {
return doPost(url,
null);
}
/**
* POST请求,带参数
*
* @param url
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public HttpResult
doPost(String url, Map<String, String> params)
throws ClientProtocolException, IOException {
HttpPost httpPost =
new HttpPost(url);
if (
null != params) {
ArrayList<NameValuePair> pairs =
new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
pairs.add(
new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity entity =
new UrlEncodedFormEntity(pairs);
httpPost.setEntity(entity);
}
httpPost.setConfig(
this.requestConfig);
httpPost.setHeader(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");
CloseableHttpResponse response =
null;
try {
response = getHttpClient().execute(httpPost);
return new HttpResult(response.getStatusLine().getStatusCode(),
EntityUtils.toString(response.getEntity(),
"UTF-8"));
}
finally {
if (response !=
null) {
response.close();
}
}
}
/**
* POST请求,提交json数据
*
* @param url
* @param json
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public HttpResult
doPostJson(String url, String json)
throws ClientProtocolException, IOException {
HttpPost httpPost =
new HttpPost(url);
httpPost.setConfig(
this.requestConfig);
if (json !=
null) {
StringEntity stringEntity =
new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(stringEntity);
}
CloseableHttpResponse response =
null;
try {
response =
this.getHttpClient().execute(httpPost);
return new HttpResult(response.getStatusLine().getStatusCode(),
EntityUtils.toString(response.getEntity(),
"UTF-8"));
}
finally {
if (response !=
null) {
response.close();
}
}
}
}
http执行post请求返回的封装类
package com.taotao.web.httpclient;
/**
* http执行post请求返回的封装类
* @author huangyk
* Created on 2016年12月2日 下午3:38:27
*/
public class HttpResult {
private Integer code;
private String body;
public HttpResult() {
}
public HttpResult(Integer code, String body) {
this.code = code;
this.body = body;
}
public Integer
getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String
getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
定期清理无效连接线程
package com.taotao.web.httpclient;
import org.apache.http.conn.HttpClientConnectionManager;
/**
* 定期清理无效连接线程
* @author huangyk
* Created on 2016年12月2日 下午3:35:25
*/
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();
}
}
}