package com.qianwen.mdc.collect.utils;
|
|
import java.io.IOException;
|
import java.io.UnsupportedEncodingException;
|
import java.net.URLEncoder;
|
import java.nio.charset.StandardCharsets;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.concurrent.TimeUnit;
|
import org.apache.http.HttpEntity;
|
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.config.Registry;
|
import org.apache.http.config.RegistryBuilder;
|
import org.apache.http.conn.HttpClientConnectionManager;
|
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.CloseableHttpClient;
|
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.BasicNameValuePair;
|
import org.apache.http.util.EntityUtils;
|
|
/* loaded from: mdc-plugin-0.0.1-SNAPSHOT-删除lib.jar:BOOT-INF/classes/cn/net/hx/mdc/utils/PooledHttpClientUtil.class */
|
public class PooledHttpClientUtil {
|
private static final int DEFAULT_POOL_MAX_TOTAL = 200;
|
private static final int DEFAULT_POOL_MAX_PER_ROUTE = 200;
|
private static final int DEFAULT_CONNECT_TIMEOUT = 500;
|
private static final int DEFAULT_CONNECT_REQUEST_TIMEOUT = 500;
|
private static final int DEFAULT_SOCKET_TIMEOUT = 2000;
|
private final int maxTotal;
|
private final int maxPerRoute;
|
private final int connectTimeout;
|
private final int connectRequestTimeout;
|
private final int socketTimeout;
|
private PoolingHttpClientConnectionManager gcm;
|
private CloseableHttpClient httpClient;
|
private IdleConnectionMonitorThread idleThread;
|
|
public PooledHttpClientUtil() {
|
this(200, 200, 500, 500, DEFAULT_SOCKET_TIMEOUT);
|
}
|
|
public PooledHttpClientUtil(int maxTotal, int maxPerRoute, int connectTimeout, int connectRequestTimeout, int socketTimeout) {
|
this.maxTotal = maxTotal;
|
this.maxPerRoute = maxPerRoute;
|
this.connectTimeout = connectTimeout;
|
this.connectRequestTimeout = connectRequestTimeout;
|
this.socketTimeout = socketTimeout;
|
//Registry<ConnectionSocketFactory> registry = RegistryBuilder.create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", SSLConnectionSocketFactory.getSocketFactory()).build();
|
//TODO 修改,增加泛型参数,不知道对不对
|
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", SSLConnectionSocketFactory.getSocketFactory()).build();
|
|
this.gcm = new PoolingHttpClientConnectionManager(registry);
|
this.gcm.setMaxTotal(this.maxTotal);
|
this.gcm.setDefaultMaxPerRoute(this.maxPerRoute);
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(this.connectTimeout).setSocketTimeout(this.socketTimeout).setConnectionRequestTimeout(this.connectRequestTimeout).build();
|
HttpClientBuilder httpClientBuilder = HttpClients.custom();
|
this.httpClient = httpClientBuilder.setConnectionManager(this.gcm).setDefaultRequestConfig(requestConfig).build();
|
this.idleThread = new IdleConnectionMonitorThread(this.gcm);
|
this.idleThread.start();
|
}
|
|
public String doGet(String url) {
|
return doGet(url, Collections.EMPTY_MAP, Collections.EMPTY_MAP);
|
}
|
|
public String doGet(String url, Map<String, Object> params) {
|
return doGet(url, Collections.EMPTY_MAP, params);
|
}
|
|
public String doGet(String url, Map<String, String> headers, Map<String, Object> params) {
|
HttpEntity entityRes;
|
HttpGet httpGet = params == null ? new HttpGet(url) : new HttpGet(getUrlWithParams(url, params));
|
if (headers != null && headers.size() > 0) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
httpGet.addHeader(entry.getKey(), entry.getValue());
|
}
|
}
|
CloseableHttpResponse response = null;
|
try {
|
response = this.httpClient.execute(httpGet);
|
if (response == null || response.getStatusLine() == null) {
|
if (response != null) {
|
try {
|
response.close();
|
} catch (IOException e) {
|
}
|
}
|
return null;
|
}
|
int statusCode = response.getStatusLine().getStatusCode();
|
if (statusCode != 200 || (entityRes = response.getEntity()) == null) {
|
if (response != null) {
|
try {
|
response.close();
|
} catch (IOException e2) {
|
}
|
}
|
return null;
|
}
|
String entityUtils = EntityUtils.toString(entityRes, "UTF-8");
|
if (response != null) {
|
try {
|
response.close();
|
} catch (IOException e3) {
|
}
|
}
|
return entityUtils;
|
} catch (IOException e4) {
|
if (response != null) {
|
try {
|
response.close();
|
return null;
|
} catch (IOException e5) {
|
return null;
|
}
|
}
|
return null;
|
} catch (Throwable th) {
|
if (response != null) {
|
try {
|
response.close();
|
} catch (IOException e6) {
|
}
|
}
|
throw th;
|
}
|
}
|
|
public String doPost(String apiUrl, Map<String, Object> params) {
|
return doPost(apiUrl, Collections.EMPTY_MAP, params);
|
}
|
|
public String doPost(String apiUrl, Map<String, String> headers, Map<String, Object> params) {
|
HttpEntity entityRes;
|
HttpPost httpPost = new HttpPost(apiUrl);
|
if (headers != null && headers.size() > 0) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
httpPost.addHeader(entry.getKey(), entry.getValue());
|
}
|
}
|
if (params != null && params.size() > 0) {
|
HttpEntity entityReq = getUrlEncodedFormEntity(params);
|
httpPost.setEntity(entityReq);
|
}
|
CloseableHttpResponse response = null;
|
try {
|
response = this.httpClient.execute(httpPost);
|
if (response == null || response.getStatusLine() == null) {
|
if (response != null) {
|
try {
|
response.close();
|
} catch (IOException e) {
|
}
|
}
|
return null;
|
}
|
int statusCode = response.getStatusLine().getStatusCode();
|
if (statusCode != 200 || (entityRes = response.getEntity()) == null) {
|
if (response != null) {
|
try {
|
response.close();
|
} catch (IOException e2) {
|
}
|
}
|
return null;
|
}
|
String entityUtils = EntityUtils.toString(entityRes, "UTF-8");
|
if (response != null) {
|
try {
|
response.close();
|
} catch (IOException e3) {
|
}
|
}
|
return entityUtils;
|
} catch (IOException e4) {
|
if (response != null) {
|
try {
|
response.close();
|
return null;
|
} catch (IOException e5) {
|
return null;
|
}
|
}
|
return null;
|
} catch (Throwable th) {
|
if (response != null) {
|
try {
|
response.close();
|
} catch (IOException e6) {
|
}
|
}
|
throw th;
|
}
|
}
|
|
private HttpEntity getUrlEncodedFormEntity(Map<String, Object> params) {
|
List<NameValuePair> pairList = new ArrayList<>(params.size());
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
pairList.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
|
}
|
return new UrlEncodedFormEntity(pairList, StandardCharsets.UTF_8);
|
}
|
|
private String getUrlWithParams(String url, Map<String, Object> params) {
|
boolean first = true;
|
StringBuilder sb = new StringBuilder(url);
|
for (String key : params.keySet()) {
|
char ch = '&';
|
if (first) {
|
ch = '?';
|
first = false;
|
}
|
String value = params.get(key).toString();
|
try {
|
String sval = URLEncoder.encode(value, "UTF-8");
|
sb.append(ch).append(key).append("=").append(sval);
|
} catch (UnsupportedEncodingException e) {
|
}
|
}
|
return sb.toString();
|
}
|
|
public void shutdown() {
|
this.idleThread.shutdown();
|
}
|
|
/* loaded from: mdc-plugin-0.0.1-SNAPSHOT-删除lib.jar:BOOT-INF/classes/cn/net/hx/mdc/utils/PooledHttpClientUtil$IdleConnectionMonitorThread.class */
|
private class IdleConnectionMonitorThread extends Thread {
|
private final HttpClientConnectionManager connMgr;
|
private volatile boolean exitFlag = false;
|
|
public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr) {
|
this.connMgr = connMgr;
|
setDaemon(true);
|
}
|
|
@Override // java.lang.Thread, java.lang.Runnable
|
public void run() {
|
while (!this.exitFlag) {
|
synchronized (this) {
|
try {
|
wait(2000L);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
this.connMgr.closeExpiredConnections();
|
this.connMgr.closeIdleConnections(30L, TimeUnit.SECONDS);
|
}
|
}
|
|
public void shutdown() {
|
this.exitFlag = true;
|
synchronized (this) {
|
notify();
|
}
|
}
|
}
|
}
|