1. 依賴包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
</dependency>
2. 代碼
import org.apache.http.*;
import org.apache.http.client.HttpRequestRetryHandler;
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.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
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.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpClientUtil {
// org.apache.http.impl.client.CloseableHttpClient
private static CloseableHttpClient httpclient = null;
// 這里就直接默認固定了,因為以下三個參數在新建的method中仍然可以重新配置並被覆蓋.
static final int connectionRequestTimeout = 5000;// ms毫秒,從池中獲取鏈接超時時間
static final int connectTimeout = 5000;// ms毫秒,建立鏈接超時時間
static final int socketTimeout = 30000;// ms毫秒,讀取超時時間
// 總配置,主要涉及是以下兩個參數,如果要作調整沒有用到properties會比較后麻煩,但鑒於一經粘貼,隨處可用的特點,就不再做依賴性配置化處理了.
// 而且這個參數同一家公司基本不會變動.
static final int maxTotal = 500;// 最大總並發,很重要的參數
static final int maxPerRoute = 100;// 每路並發,很重要的參數
// 正常情況這里應該配成MAP或LIST
// 細化配置參數,用來對每路參數做精細化處理,可以管控各ip的流量,比如默認配置請求baidu:80端口最大100個並發鏈接,
static final String detailHostName = "http://www.baidu.com";// 每個細化配置之ip(不重要,在特殊場景很有用)
static final int detailPort = 80;// 每個細化配置之port(不重要,在特殊場景很有用)
static final int detailMaxPerRoute = 100;// 每個細化配置之最大並發數(不重要,在特殊場景很有用)
private static CloseableHttpClient getHttpClient() {
if (null == httpclient) {
synchronized (HttpClientUtil.class) {
if (null == httpclient) {
httpclient = init();
}
}
}
return httpclient;
}
/**
* 鏈接池初始化 這里最重要的一點理解就是. 讓CloseableHttpClient 一直活在池的世界里, 但是HttpPost卻一直用完就消掉.
* 這樣可以讓鏈接一直保持着.
*/
private static CloseableHttpClient init() {
CloseableHttpClient newHttpclient;
// 設置連接池
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", plainsf).register("https", sslsf).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
// 將最大連接數增加
cm.setMaxTotal(maxTotal);
// 將每個路由基礎的連接增加
cm.setDefaultMaxPerRoute(maxPerRoute);
// 細化配置開始,其實這里用Map或List的for循環來配置每個鏈接,在特殊場景很有用.
// 將每個路由基礎的連接做特殊化配置,一般用不着
HttpHost httpHost = new HttpHost(detailHostName, detailPort);
// 將目標主機的最大連接數增加
cm.setMaxPerRoute(new HttpRoute(httpHost), detailMaxPerRoute);
// 細化配置結束
// 請求重試處理
HttpRequestRetryHandler httpRequestRetryHandler = (exception, executionCount, context) -> {
if (executionCount >= 2) {// 如果已經重試了2次,就放棄
return false;
}
if (exception instanceof NoHttpResponseException) {// 如果服務器丟掉了連接,那么就重試
return true;
}
if (exception instanceof SSLHandshakeException) {// 不要重試SSL握手異常
return false;
}
if (exception instanceof InterruptedIOException) {// 超時
return false;
}
if (exception instanceof UnknownHostException) {// 目標服務器不可達
return false;
}
if (exception instanceof SSLException) {// SSL握手異常
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
// 如果請求是冪等的,就再次嘗試
return !(request instanceof HttpEntityEnclosingRequest);
};
// 配置請求的超時設置
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout).setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout).build();
newHttpclient = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(requestConfig).setRetryHandler(httpRequestRetryHandler).build();
return newHttpclient;
}
public static String doGet(String url, Map<String, String> param) {
// 創建Httpclient對象
CloseableHttpClient httpclient = getHttpClient();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 創建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
// 創建http GET請求
HttpGet httpGet = new HttpGet(uri);
// 執行請求
response = httpclient.execute(httpGet);
// 判斷返回狀態是否為200
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doGet(String url) {
return doGet(url, null);
}
public static String doPost(String url, Map<String, String> param) {
// 創建Httpclient對象
CloseableHttpClient httpClient = getHttpClient();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 創建Http Post請求
HttpPost httpPost = new HttpPost(url);
// 創建參數列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模擬表單
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8");
httpPost.setEntity(entity);
}
// 執行http請求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doPost(String url) {
return doPost(url, null);
}
public static String doPostJson(String url, String json) {
// 創建Httpclient對象
CloseableHttpClient httpClient = getHttpClient();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 創建Http Post請求
HttpPost httpPost = new HttpPost(url);
// 創建請求內容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 執行http請求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
}