项目场景:
老项目对接外部服务(htts类型),https的访问协议要TLSv1.2,而为了保持项目的稳定性,jdk不可随意升级,后查找资料发现可通过设置协议解决
解决方案:
SSLContexts中设置使用的协议TLSv1.2
private String sendPushHttpPost(String url, String regJson) throws Exception {
SSLContext ctx = SSLContexts.custom().useProtocol("TLSv1.2").build();
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(new SSLConnectionSocketFactory(ctx)).build();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-type","application/json;charset=utf-8");
httpPost.setHeader("Accept", "application/json");
StringEntity uefEntity = new StringEntity(regJson, "UTF-8");
httpPost.setEntity(uefEntity);
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, "UTF-8");
response.close();
httpClient.close();
return responseContent;
}