依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.15</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.13</version>
</dependency>
HttpClientUtil.java
package ppp;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.NameValuePair;
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.conn.ConnectionKeepAliveStrategy;
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.BasicHeaderElementIterator;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* 引入单例连接池
*/
@Slf4j
public class HttpClientUtil {
private static CloseableHttpClient httpClient;
private static RequestConfig config;
private static class Holder {
private static final HttpClientUtil INSTANCE = new HttpClientUtil();
}
public static HttpClientUtil me() {
return Holder.INSTANCE;
}
private HttpClientUtil() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
//最大连接数
connectionManager.setMaxTotal(200);
// 并发数
connectionManager.setDefaultMaxPerRoute(10);
// 检查间隔为5秒
connectionManager.setValidateAfterInactivity(5000);
// 关闭空闲连接的时间为60S
connectionManager.closeIdleConnections(60, TimeUnit.SECONDS);
config = RequestConfig.custom()
// 从连接池中取连接超时时间
.setConnectionRequestTimeout(5000)
// 建立链接超时时间
.setConnectTimeout(5000)
// 等待读取数据时间
.setSocketTimeout(20000)
.build();
httpClient = HttpClients.custom()
// 关闭自动重试
.disableAutomaticRetries()
//长连接
// .setKeepAliveStrategy( keepAliveStrategy() )
// 设置连接池
.setConnectionManager(connectionManager)
// 设置连接自动回收时间 连接池配置了,这个就不需要配置了
//.evictIdleConnections(60, TimeUnit.SECONDS)
// 设置超时时间
.setDefaultRequestConfig( config )
.build();
}
//长连接
private ConnectionKeepAliveStrategy genKeepAliveStrategy() {
return (response, context) -> {
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
if ("timeout".equalsIgnoreCase(he.getName())) {
return Long.parseLong(he.getValue()) * 1000;
}
}
return 60 * 1000; // 默认保持活动时间为60秒
};
}
/**
* 不带参数的get请求,如果状态码为200,则返回body,如果不为200,则返回null
*
* @param url
*/
public String doGet(String url) {
log.info("httpGet_url==>{}", url);
// 声明 http get 请求
HttpGet httpGet = new HttpGet(url);
// 装载配置信息
httpGet.setConfig( config);
// 发起请求
try {
CloseableHttpResponse response = httpClient.execute(httpGet);
// 判断状态码是否为200
if (response.getStatusLine().getStatusCode() == 200) {
// 返回响应体的内容
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
log.info("httpGet_result==>{}", result);
return result;
}
} catch (Exception e) {
log.error("httpGet_error==>", e);
return null;
}
return null;
}
/**
* 带参数的get请求,如果状态码为200,则返回body,如果不为200,则返回null
*
* @param url
*/
public String doGet(String url, Map<String, Object> map) {
URIBuilder uriBuilder = null;
try {
uriBuilder = new URIBuilder(url);
if (map != null) {
// 遍历map,拼接请求参数
for (Map.Entry<String, Object> entry : map.entrySet()) {
uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
}
}
return doGet(uriBuilder.build().toString());
} catch (Exception e) {
return null;
}
}
/**
* 不带参数post请求
*
* @param url
* @return string json串
* @throws Exception
*/
public String doPost(String url) throws Exception {
return doPost(url, null, null);
}
public String doPost(String url, Map<String, Object> map) throws Exception {
return doPost(url, map, null);
}
/**
* 带map参数的post请求
*
* @param url
* @param map
* @return string json串
* @throws Exception
*/
public String doPost(String url, Map<String, Object> map, Map<String, String> headers) throws Exception {
log.info("httpPost_url==>{},params==>{}", url, map);
// 声明httpPost请求
HttpPost httpPost = new HttpPost(url);
// 加入配置信息
httpPost.setConfig( config);
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
// 判断map是否为空,不为空则进行遍历,封装from表单对象
if (map != null) {
List<NameValuePair> list = new ArrayList<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
list.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
// 构造from表单对象
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "UTF-8");
// 把表单放到post里
httpPost.setEntity(urlEncodedFormEntity);
}
// 发起请求
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
if (response.getStatusLine().getStatusCode() == 200) {
// 返回响应体的内容
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
log.info("httpPost_result==>{}", result);
return result;
}
}
return null;
}
/**
* 带json参数的post请求,用的最多,因为map可以转为json
*
* @param url 请求地址host+path
* @param json json字符串
* @return string json串
*/
public String doPost(String url, String json) {
log.info("httpPost_url==>{},json==>{}", url, json);
//设置请求路径,请求格式,配置信息
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
httpPost.setConfig(config);
// 发起请求
try {
//格式化请求数据并设值
StringEntity se = new StringEntity(json, "UTF-8");
se.setContentType("text/json");
httpPost.setEntity(se);
CloseableHttpResponse response = httpClient.execute(httpPost);
// 判断状态码是否为200
if (response.getStatusLine().getStatusCode() == 200) {
// 返回响应体的内容
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
log.info("httpPost_result==>{}", result);
return result;
}
} catch (Exception e) {
log.error("httpPost_error==>", e);
return null;
}
return null;
}
}
如果需要支持https
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslConnectionSocketFactory())
.build();
PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);
//最大连接数
httpClientConnectionManager.setMaxTotal(100);
//并发数
httpClientConnectionManager.setDefaultMaxPerRoute(10);
/**
* 支持SSL
*/
private SSLConnectionSocketFactory sslConnectionSocketFactory() {
try {
return new SSLConnectionSocketFactory(
SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(),
NoopHostnameVerifier.INSTANCE);
} catch (Exception e) {
return SSLConnectionSocketFactory.getSocketFactory();
}
}
如果需要修改请求重试
httpClient = HttpClientBuilder.create()
// 关闭自动重试
// .disableAutomaticRetries()
// 自定义自动重试
.setRetryHandler(httpRequestRetryHandler())
// 设置连接池
.setConnectionManager(connectionManager)
// setConnectionTimeToLive(2, TimeUnit.MINUTES) 设置链接最大存活时间 此选项无效
// 设置连接自动回收时间
.evictIdleConnections(1, TimeUnit.MINUTES)
// 设置超时时间
.setDefaultRequestConfig(requestConfig).build();
/**
* 自动重试
*/
private HttpRequestRetryHandler httpRequestRetryHandler() {
return (exception, executionCount, context) -> {
if (executionCount >= 5) {// 如果已经重试了5次,就放弃
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);
};
}