import org.apache.*;
public class HttpClientHelper {
private static final String APPLICATION_X_WWW_FORM_URLENCODED = ContentType.APPLICATION_FORM_URLENCODED.getMimeType();
private static final String APPLICATION_JSON = ContentType.APPLICATION_JSON.getMimeType();
private static final String CHARTSET = "UTF-8";
private static final int CONNTIMEOUT = 60000;
private static final int READTIMEOUT = 60000;
private static final int MAX_RETRY = 3;
private static CloseableHttpClient httpClient = null;
static {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
Registry registry = RegistryBuilder.create().register("http", plainsf)
.register("https", sslsf).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
// 将最大连接数增加到1000
cm.setMaxTotal(50);
// 将每个路由基础的连接增加到100
cm.setDefaultMaxPerRoute(10);
httpClient = HttpClients.custom().setConnectionManager(cm).build();
}
public static T postForObject(String url, String body, Class toClass) throws IOException {
String res = post(url, body, APPLICATION_X_WWW_FORM_URLENCODED, CHARTSET, CONNTIMEOUT, READTIMEOUT);
return JsonUtils.parse(res, toClass);
}
public static Map, ?> postForMap(String url, String body) throws IOException {
String res = post(url, body, APPLICATION_X_WWW_FORM_URLENCODED, CHARTSET, CONNTIMEOUT, READTIMEOUT);
return JsonUtils.parse(res,Map.class);
}
public static String post(String url, String body) throws IOException {
return post(url, body, APPLICATION_X_WWW_FORM_URLENCODED, CHARTSET, CONNTIMEOUT, READTIMEOUT);
}
public static String post(String url, String body, String charset, Integer connTimeout, Integer readTimeout) throws IOException {
return post(url, body, APPLICATION_X_WWW_FORM_URLENCODED, charset, connTimeout, readTimeout);
}
public static T postJsonData(String url, String body, Class toClass) throws IOException {
String res = post(url, body, APPLICATION_JSON, CHARTSET, CONNTIMEOUT, READTIMEOUT);
return JsonUtils.parse(res, toClass);
}
public static T postFormData(String url, Map params, Class toClass) throws IOException {
String res = postForm(url, params, null, CONNTIMEOUT, READTIMEOUT);
return JsonUtils.parse(res, toClass);
}
public static T postFormData(String url, Map params, Map headers, Class toClass) throws IOException {
String res = postForm(url, params, headers, CONNTIMEOUT, READTIMEOUT);
return JsonUtils.parse(res, toClass);
}
public static String postFormData(String url, Map params) throws IOException {
return postForm(url, params, null, CONNTIMEOUT, READTIMEOUT);
}
public static String postFormData(String url, Map params, Integer connTimeout, Integer readTimeout) throws IOException {
return postForm(url, params, null, connTimeout, readTimeout);
}
public static T getForObject(String url, Class toClass) throws UnsupportedOperationException, IOException {
String res = get(url, CHARTSET, CONNTIMEOUT, READTIMEOUT);
return JsonUtils.parse(res, toClass);
}
public static String get(String url) throws IOException {
return get(url, CHARTSET, CONNTIMEOUT, READTIMEOUT);
}
public static String get(String url, String charset) throws IOException {
return get(url, charset, CONNTIMEOUT, READTIMEOUT);
}
public static String post(String url, String body, String mimeType, String charset, Integer connTimeout, Integer readTimeout)
throws IOException {
HttpPost post = new HttpPost(url);
String result = null;
try {
if (StringUtils.isNotBlank(body)) {
HttpEntity entity = new StringEntity(body, ContentType.create(mimeType, charset));
post.setEntity(entity);
}
RequestConfig customReqConfig = getCustomReqConfig(connTimeout, readTimeout);
post.setConfig(customReqConfig);
HttpResponse res = httpClient.execute(post);
result = EntityUtils.toString(res.getEntity(), charset);
} finally {
post.releaseConnection();
}
return result;
}
public static String postForm(String url, Map params, Map headers, Integer connTimeout,
Integer readTimeout) throws IOException {
HttpPost post = new HttpPost(url);
try {
if (params != null && !params.isEmpty()) {
List formParams = new ArrayList<>();
Set> entrySet = params.entrySet();
for (Entry entry : entrySet) {
formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, Consts.UTF_8);
post.setEntity(entity);
}
if (headers != null && !headers.isEmpty()) {
for (Entry entry : headers.entrySet()) {
post.addHeader(entry.getKey(), entry.getValue());
}
}
RequestConfig customReqConfig = getCustomReqConfig(connTimeout, readTimeout);
post.setConfig(customReqConfig);
HttpResponse res = httpClient.execute(post);
return EntityUtils.toString(res.getEntity(), CHARTSET);
} finally {
post.releaseConnection();
}
}
public static String get(String url, String charset, Integer connTimeout, Integer readTimeout) throws UnsupportedOperationException,
IOException {
HttpGet get = new HttpGet(url);
String result = "";
try {
RequestConfig customReqConfig = getCustomReqConfig(connTimeout, readTimeout);
get.setConfig(customReqConfig);
HttpResponse res = httpClient.execute(get);
result = EntityUtils.toString(res.getEntity(), charset);
} finally {
get.releaseConnection();
}
return result;
}
private static RequestConfig getCustomReqConfig(Integer connTimeout, Integer readTimeout) {
Builder customReqConf = RequestConfig.custom();
if (connTimeout != null) {
customReqConf.setConnectTimeout(connTimeout);
}
if (readTimeout != null) {
customReqConf.setSocketTimeout(readTimeout);
}
return customReqConf.build();
}
}