新旧apache HttpClient 获取httpClient方法

在apache httpclient 4.3版本中对很多旧的类进行了deprecated标注,通常比较常用的就是下面两个类了。

DefaultHttpClient —> CloseableHttpClient
HttpResponse —> CloseableHttpResponse

目前互联网对外提供的接口通常都是HTTPS协议,有时候接口提供方所示用的证书会出现证书不受信任的提示,chrome访问接口(通常也不会用chrome去访问接口,只是举个例子)会出现这样的提示:

为此我们调用这类接口的时候就要忽略掉证书认证信息,我们调用接口的httpClient就要做特殊处理。下面记录下httpclient 4.3以前和之后的httpClient获取方法。

httpclient jar包4.3以前版本获取HttpClient方法如下:

 1 public static HttpClient getHttpClient(HttpClient base) {
 2         try {
 3             SSLContext ctx = SSLContext.getInstance("SSL");
 4             X509TrustManager tm = new X509TrustManager() {
 5                 public java.security.cert.X509Certificate[] getAcceptedIssuers() {
 6                     return null;
 7                 }
 8 
 9                 @Override
10                 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
11                 }
12 
13                 @Override
14                 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
15                 }
16             };
17 
18             ctx.init(null, new TrustManager[] {tm}, null);
19             SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
20             ClientConnectionManager mgr = base.getConnectionManager();
21             SchemeRegistry registry = mgr.getSchemeRegistry();
22             registry.register(new Scheme("https", 443, ssf));
23             return new DefaultHttpClient(mgr, base.getParams());
24         } catch (Exception e) {
25             log.warn("{}", e);
26             return null;
27         }
28     }

httpclient jar包4.3之后版本获取HttpClient方法如下:

 1 public static CloseableHttpClient getHttpClient() {
 2         try {
 3             SSLContext sslContext = SSLContext.getInstance("SSL");
 4             sslContext.init(null, new TrustManager[] {new X509TrustManager() {
 5                 @Override
 6                 public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
 7 
 8                 }
 9 
10                 @Override
11                 public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
12 
13                 }
14 
15                 @Override
16                 public X509Certificate[] getAcceptedIssuers() {
17                     return new X509Certificate[0];
18                 }
19             }}, new SecureRandom());
20             SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
21             CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().setSSLSocketFactory(socketFactory).build();
22             return closeableHttpClient;
23         } catch (Exception e) {
24             log.warn("create closeable http client failed!");
25             return HttpClientBuilder.create().build();
26         }
27     }
原文地址:https://www.cnblogs.com/snowater/p/7591128.html