|
|
|
@ -54,7 +54,7 @@ public class HttpsUtils { |
|
|
|
|
// 设置连接池
|
|
|
|
|
connMgr = new PoolingHttpClientConnectionManager(); |
|
|
|
|
// 设置连接池大小
|
|
|
|
|
connMgr.setMaxTotal(100); |
|
|
|
|
connMgr.setMaxTotal(5000); |
|
|
|
|
connMgr.setDefaultMaxPerRoute(connMgr.getMaxTotal()); |
|
|
|
|
// Validate connections after 1 sec of inactivity
|
|
|
|
|
connMgr.setValidateAfterInactivity(1000); |
|
|
|
@ -100,7 +100,8 @@ public class HttpsUtils { |
|
|
|
|
} |
|
|
|
|
apiUrl += param; |
|
|
|
|
String result = null; |
|
|
|
|
HttpClient httpClient = null; |
|
|
|
|
CloseableHttpClient httpClient; |
|
|
|
|
CloseableHttpResponse response = null; |
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
if (apiUrl.startsWith("https")) { |
|
|
|
@ -111,7 +112,7 @@ public class HttpsUtils { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
HttpGet httpGet = new HttpGet(apiUrl); |
|
|
|
|
HttpResponse response = httpClient.execute(httpGet); |
|
|
|
|
response = httpClient.execute(httpGet); |
|
|
|
|
HttpEntity entity = response.getEntity(); |
|
|
|
|
if (entity != null) { |
|
|
|
|
InputStream instream = entity.getContent(); |
|
|
|
@ -120,48 +121,19 @@ public class HttpsUtils { |
|
|
|
|
return JSON.parseObject(result); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error(e.getMessage(),e); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static JSONObject doGet(String url, Map<String, String> params , String o) { |
|
|
|
|
String apiUrl = url; |
|
|
|
|
StringBuffer param = new StringBuffer(); |
|
|
|
|
int i = 0; |
|
|
|
|
for (String key : params.keySet()) { |
|
|
|
|
if (i == 0) |
|
|
|
|
param.append("?"); |
|
|
|
|
else |
|
|
|
|
param.append("&"); |
|
|
|
|
param.append(key).append("=").append(params.get(key)); |
|
|
|
|
i++; |
|
|
|
|
} |
|
|
|
|
apiUrl += param; |
|
|
|
|
String result = null; |
|
|
|
|
HttpClient httpClient = null; |
|
|
|
|
} finally { |
|
|
|
|
if (response != null) { |
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
if (apiUrl.startsWith("https")) { |
|
|
|
|
httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()) |
|
|
|
|
.setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build(); |
|
|
|
|
} else { |
|
|
|
|
httpClient = HttpClients.createDefault(); |
|
|
|
|
EntityUtils.consume(response.getEntity()); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
log.error(e.getMessage(),e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
HttpGet httpGet = new HttpGet(apiUrl); |
|
|
|
|
HttpResponse response = httpClient.execute(httpGet); |
|
|
|
|
HttpEntity entity = response.getEntity(); |
|
|
|
|
if (entity != null) { |
|
|
|
|
InputStream instream = entity.getContent(); |
|
|
|
|
result = IOUtils.toString(instream, "UTF-8"); |
|
|
|
|
} |
|
|
|
|
return JSON.parseObject(result); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error(e.getMessage(),e); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static JSONObject doGet(String url, Map<String, String> params , Map<String , String> headers) { |
|
|
|
|
String apiUrl = url; |
|
|
|
|
StringBuffer param = new StringBuffer(); |
|
|
|
@ -176,7 +148,8 @@ public class HttpsUtils { |
|
|
|
|
} |
|
|
|
|
apiUrl += param; |
|
|
|
|
String result = null; |
|
|
|
|
HttpClient httpClient = null; |
|
|
|
|
CloseableHttpClient httpClient = null; |
|
|
|
|
CloseableHttpResponse response = null; |
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
if (apiUrl.startsWith("https")) { |
|
|
|
@ -190,7 +163,7 @@ public class HttpsUtils { |
|
|
|
|
for (Map.Entry<String, String> e : headers.entrySet()) { |
|
|
|
|
httpGet.addHeader(e.getKey(), e.getValue()); |
|
|
|
|
} |
|
|
|
|
HttpResponse response = httpClient.execute(httpGet); |
|
|
|
|
response = httpClient.execute(httpGet); |
|
|
|
|
HttpEntity entity = response.getEntity(); |
|
|
|
|
if (entity != null) { |
|
|
|
|
InputStream instream = entity.getContent(); |
|
|
|
@ -199,6 +172,14 @@ public class HttpsUtils { |
|
|
|
|
return JSON.parseObject(result); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error(e.getMessage(),e); |
|
|
|
|
} finally { |
|
|
|
|
if (response != null) { |
|
|
|
|
try { |
|
|
|
|
EntityUtils.consume(response.getEntity()); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
log.error(e.getMessage(),e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
@ -223,7 +204,7 @@ public class HttpsUtils { |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static JSONObject doPost(String apiUrl, Map<String, Object> params) { |
|
|
|
|
CloseableHttpClient httpClient = null; |
|
|
|
|
CloseableHttpClient httpClient; |
|
|
|
|
if (apiUrl.startsWith("https")) { |
|
|
|
|
httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()) |
|
|
|
|
.setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build(); |
|
|
|
@ -349,7 +330,7 @@ public class HttpsUtils { |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static JSONObject doPost(String apiUrl, String str) { |
|
|
|
|
CloseableHttpClient httpClient = null; |
|
|
|
|
CloseableHttpClient httpClient; |
|
|
|
|
if (apiUrl.startsWith("https")) { |
|
|
|
|
httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()) |
|
|
|
|
.setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build(); |
|
|
|
|