基于RestTemplate的HttpUtil,支持https请求和连接池配置
package com.XXX.common.util;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.protocol.HTTP;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Http工具类,基于RestTemplate
* @author yuht
* @date 2019/3/22
*/
@Slf4j
public class HttpClientUtil {
//连接建立的超时间
private static int connectTimeout = 1000;
//等待数据返回的超时时间
private static int socketTimeout = 10000;
//从连接池中获取链接的超时时间
private static int connectionRequestTimeout = 1000;
//连接池的最大连接数
private static int maxTotalConnections = 200;
private static int defaultMaxPerRoute = 20;
private static int defaultKeepAliveTimeMillis = 20000;
private final static RestTemplate restTemplate;
static {
restTemplate = new RestTemplate(httpRequestFactory());
List> messageConverters = Lists.newArrayList();
messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(new ResourceHttpMessageConverter(false));
messageConverters.add(new SourceHttpMessageConverter());
messageConverters.add(new AllEncompassingFormHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
messageConverters.add(new FastJsonHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
}
private static ClientHttpRequestFactory httpRequestFactory() {
return new HttpComponentsClientHttpRequestFactory(httpClient());
}
private static ConnectionKeepAliveStrategy connectionKeepAliveStrategy() {
return ((response, httpContext) -> {
HeaderElementIterator it = new BasicHeaderElementIterator
(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
return Long.parseLong(value) * 1000;
}
}
return defaultKeepAliveTimeMillis;
});
}
private static PoolingHttpClientConnectionManager poolingConnectionManager() {
SSLContextBuilder builder = new SSLContextBuilder();
try {
builder.loadTrustMaterial(null, (x509Certificates, string) -> true);
} catch (NoSuchAlgorithmException | KeyStoreException e) {
log.error("Pooling Connection Manager Initialisation failure because of {}", ExceptionUtils.getStackTrace(e), e);
}
SSLConnectionSocketFactory sslsf = null;
try {
sslsf = new SSLConnectionSocketFactory(builder.build());
} catch (KeyManagementException | NoSuchAlgorithmException e) {
log.error("Pooling Connection Manager Initialisation failure because of " + e.getMessage(), e);
}
Registry socketFactoryRegistry = RegistryBuilder
.create()
.register("https", sslsf)
.register("http", new PlainConnectionSocketFactory())
.build();
PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
//最大连接数
poolingConnectionManager.setMaxTotal(maxTotalConnections);
//同路由并发数
poolingConnectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
return poolingConnectionManager;
}
private static HttpClient httpClient() {
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectTimeout)
.setSocketTimeout(socketTimeout).build();
List headers = new ArrayList<>();
headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36"));
headers.add(new BasicHeader("Accept-Encoding", "gzip,deflate"));
headers.add(new BasicHeader("Accept-Language", "zh-CN"));
headers.add(new BasicHeader("Connection", "Keep-Alive"));
// DefaultHttpRequestRetryHandler 重试次数
return HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setDefaultHeaders(headers)
.setConnectionManager(poolingConnectionManager())
.setKeepAliveStrategy(connectionKeepAliveStrategy())
.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true))
.build();
}
/**
* GET请求
* @param url
* @param responseType
* @param
* @return
*/
public static T get(String url, Class responseType) {
return get(url, responseType, null);
}
/**
* GET请求
* @param url
* @param params
* @param responseType
* @param
* @return
*/
public static T get(String url, Map params, Class responseType) {
return get(url, null, params, responseType);
}
/**
* GET请求
* @param url
* @param responseType
* @param uriVariables
* @param
* @return
*/
public static T get(String url, Class responseType, Object... uriVariables) {
return get(url, null, responseType, uriVariables);
}
/**
* GET请求
* @param url
* @param headers
* @param params
* @param responseType
* @param
* @return
*/
public static T get(String url, Map headers, Map params, Class responseType) {
return get(url, headers, params, responseType, null);
}
/**
* GET请求
* @param url
* @param headers
* @param responseType
* @param uriVariables
* @param
* @return
*/
public static T get(String url, Map headers, Class responseType, Object... uriVariables) {
return get(url, headers, null, responseType, uriVariables);
}
public static T get(String url, Map headers, Map params, Class responseType, Object... uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
if (headers != null) {
headers.forEach((k, v) -> httpHeaders.add(k, String.valueOf(v)));
}
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
if (params != null) {
params.forEach((k,v)->builder.queryParam(k,v));
}
return exchange(builder.build(false).toString(), HttpMethod.GET, httpHeaders, null, responseType, uriVariables);
}
/**
* POST请求,数据以请求体的方式提交
* @param url
* @param body
* @param responseType
* @param
* @return
*/
public static T post(String url, Object body, Class responseType) {
return post(url, null, body, responseType);
}
/**
* POST请求,数据以请求体的方式提交
* @param url
* @param headers
* @param body
* @param responseType
* @param
* @return
*/
public static T post(String url, Map headers, Object body, Class responseType) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
if (headers != null) {
headers.forEach((k, v) -> httpHeaders.add(k, String.valueOf(v)));
}
return exchange(url, HttpMethod.POST, httpHeaders, body, responseType, null);
}
/**
* POST请求,数据以表单形式提交
* @param url
* @param params
* @param responseType
* @param
* @return
*/
public static T postForm(String url, Map params, Class responseType) {
return postForm(url, null, params, responseType);
}
/**
* POST请求,数据以表单形式提交
* @param url
* @param headers
* @param params
* @param responseType
* @param
* @return
*/
public static T postForm(String url, Map headers, Map params, Class responseType) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
if (headers != null) {
headers.forEach((k, v) -> httpHeaders.add(k, String.valueOf(v)));
}
MultiValueMap body = new LinkedMultiValueMap();
if (params != null) {
params.forEach((k, v) -> body.add(k, String.valueOf(v)));
}
return exchange(url, HttpMethod.GET, httpHeaders, body, responseType, null);
}
/**
* 执行HTTP请求
* @param url
* @param method
* @param headers
* @param body
* @param responseType
* @param uriVariables
* @param
* @return
*/
private static T exchange(String url, HttpMethod method, HttpHeaders headers, Object body, Class responseType, Object... uriVariables) {
HttpEntity> httpEntity = new HttpEntity(body, headers);
if (uriVariables == null)
uriVariables = new Object[]{};
if (uriVariables.length == 1 && uriVariables[0] instanceof Map) {
Map _uriVariables = (Map) uriVariables[0];
return restTemplate.exchange(url, method, httpEntity, responseType, _uriVariables).getBody();
}
return restTemplate.exchange(url, method, httpEntity, responseType, uriVariables).getBody();
}
public static void main(String[] args) {
Map params= new HashMap<>();
params.put("name", "yuzc");
params.put("age", "1");
String result = get("http://www.baidu.com", params, String.class);
System.out.println(result);
}
}