apache httpclient 4.5 兼容 http https

本文提供了一个使用Java进行HTTPS请求的示例代码,通过构建SSL上下文并信任自签名证书来获取网页内容。此示例涵盖了从创建SSL连接到读取响应实体的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 String responseContent = "";
 2 try {
 3     SSLContextBuilder contextBuilder = new SSLContextBuilder();
 4     contextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
 5     SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(contextBuilder.build());
 6     CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();
 7     HttpGet httpGet = new HttpGet(requestUrl);
 8     CloseableHttpResponse response = httpclient.execute(httpGet);
 9     try {
10         HttpEntity entity = response.getEntity();
11         if (null != entity) {
12             responseContent = EntityUtils.toString(entity, ContentType.getOrDefault(entity).getCharset());
13             EntityUtils.consume(entity);
14         }
15     } finally {
16         response.close();
17     }
18 } catch (KeyStoreException e) {
19     e.printStackTrace();
20 } catch (NoSuchAlgorithmException e) {
21     e.printStackTrace();
22 } catch (KeyManagementException e) {
23     e.printStackTrace();
24 }
25 return responseContent;

 

转载于:https://www.cnblogs.com/Zombie-Xian/p/5355295.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值