本文整合基於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>
<!-- HttpClient對象的構建器 -->
<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder">
<property name="connectionManager" ref="poolingHttpClientConnectionManager" />
</bean>
<!-- HttpClient對象 注意該對象需要設置scope="prototype":多例 -->
<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>
<!-- RequestConfig對象 -->
<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 CloseableHttpClient httpclient;
@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 {
// 創建http GET請求
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(this.requestConfig);
CloseableHttpResponse response = null;
try {
// 執行請求
response = getHttpClient().execute(httpGet);
// 判斷返回狀態是否爲200
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 {
// 創建http POST請求
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()));
}
// 構造一個form表單式的實體
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs);
// 將請求實體設置到httpPost對象中
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 {
// 創建http POST請求
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(this.requestConfig);
if (json != null) {
// 構造一個form表單式的實體
StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
// 將請求實體設置到httpPost對象中
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();
}
}
}