apache httpclient 4.5 兼容 http https

 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Apache HttpClient 发送 HTTPHTTPS 请求的示例代码: 1. 发送 HTTP 请求 ```java import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClients; public class HttpClientExample { public static void main(String[] args) throws Exception { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://www.example.com"); HttpResponse httpResponse = httpClient.execute(httpGet); System.out.println(httpResponse.getStatusLine().getStatusCode()); // 打印响应状态码 } } ``` 2. 发送 HTTPS 请求 ```java import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLContextBuilder; import org.apache.http.impl.client.HttpClients; import javax.net.ssl.SSLContext; public class HttpClientExample { public static void main(String[] args) throws Exception { SSLContext sslContext = new SSLContextBuilder() .loadTrustMaterial(null, (certificate, authType) -> true) .build(); HttpClient httpClient = HttpClients.custom() .setSSLContext(sslContext) .setSSLHostnameVerifier(new NoopHostnameVerifier()) .build(); HttpGet httpGet = new HttpGet("https://www.example.com"); HttpResponse httpResponse = httpClient.execute(httpGet); System.out.println(httpResponse.getStatusLine().getStatusCode()); // 打印响应状态码 } } ``` 注意事项: - HTTPS 请求需要设置 SSLContext 和 SSLHostnameVerifier; - SSLContext 的 TrustManager 应该接受所有证书,这不安全,建议修改为验证证书; - SSLHostnameVerifier 应该使用严格的 HostnameVerifier,这里为了简单只使用了 NoopHostnameVerifier。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值