jdk1.7下httpclient请求https

该项目场景涉及老项目对接外部HTTPS服务,要求使用TLSv1.2协议。由于系统稳定性考虑,不能直接升级JDK。为解决问题,采取了通过SSLContexts定制协议方式,设定为TLSv1.2,并创建自定义的SSLConnectionSocketFactory,结合HttpClients构建HTTP客户端执行POST请求。
摘要由CSDN通过智能技术生成

项目场景:


老项目对接外部服务(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;
   }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值