public String doGet(String url){
try {
// http请求
CloseableHttpClient httpClient = HttpClients.createDefault();
if("https".equals(protocol)){
// https 请求忽略SSL证书
httpClient = createSSLClientDefault();
}
Map<String, String> httpsSwaggerInfo = HttpClientUtil.getHttpsSwaggerInfo(httpClient,url );
return httpsSwaggerInfo.get("body");
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
// https忽略SSL证书处理
private CloseableHttpClient createSSLClientDefault() {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
//信任所有
@Override
public boolean isTrusted(X509Certificate[] xcs, String string) {
return true;
}
}).build();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", new PlainConnectionSocketFactory())
.register("https", sslConnectionSocketFactory)
.build();
PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager(
registry);
httpClientConnectionManager.setMaxTotal(ONE_HUNDRED);
HttpClientBuilder httpClientBuilder = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory);
return httpClientBuilder.setConnectionManager(httpClientConnectionManager).build();
} catch (KeyStoreException ex) {
logger.error(ex.getMessage());
} catch (NoSuchAlgorithmException ex) {
logger.error(ex.getMessage());
} catch (KeyManagementException ex) {
logger.error(ex.getMessage());
}
return HttpClients.createDefault();
}
//调用处理请求的方法
public static Map<String,String> getHttpsSwaggerInfo(CloseableHttpClient httpClient ,String url){
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(requestConfig);
Map<String, String> invokeStatusMap = sendHttpsGet(httpClient, httpGet);
return invokeStatusMap;
}
// 处理请求
private static Map<String, String> sendHttpsGet(CloseableHttpClient httpClient, HttpGet httpGet) {
Map<String, String> resultMap = new HashMap<String, String>();
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
Integer statusCode = null;
try {
httpGet.setConfig(requestConfig);
// 执行请求
dealWithAkSk(httpGet);
response = httpClient.execute(httpGet);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
statusCode = response.getStatusLine().getStatusCode();
InputStream in = entity.getContent();
in.close();
} catch (Exception e) {
logger.error("请求异常:" + e.getMessage(), e);
throw new RuntimeException((org.apache.commons.lang3.StringUtils.isBlank(e.getMessage()) ? e.toString() :
e.getMessage()));
} finally {
try {
// 关闭连接,释放资源
if (response != null) {
response.close();
}
} catch (IOException e) {
logger.error((org.apache.commons.lang3.StringUtils.isBlank(e.getMessage()) ? e.toString() :
e.getMessage()));
throw new RuntimeException((org.apache.commons.lang3.StringUtils.isBlank(e.getMessage()) ?
e.toString() : e.getMessage()));
}
}
resultMap.put("status", statusCode == null ? null : statusCode.toString());
resultMap.put("body", responseContent == null ? null : responseContent);
return resultMap;
}
// 配置连接的条件
private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(SIXTY_THOUSAND).
setConnectTimeout(SIXTY_THOUSAND)
.setConnectionRequestTimeout(SIXTY_THOUSAND).build();
版权声明:本文为dreamrealist原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。