新旧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     }
时间: 2024-12-25 17:27:03

新旧apache HttpClient 获取httpClient方法的相关文章

MySQL新旧版本ORDER BY 处理方法

MySQL 的order by 涉及到三个参数:A. sort_buffer_size 排序缓存.B. read_rnd_buffer_size 第二次排序缓存.C. max_length_for_sort_data 带普通列的最大排序约束. 我来简单说下MySQL的排序规则.假设查询语句select * from tb1 where 1 order by  a ; 字段a没有建立索引:以上三个参数都足够大.MySQL内部有两种排序规则:第一种,是普通的排序.这种排序的特点是节省内存,但是最终会

org.apache.http.client.HttpClient使用方法

官网说明: http://hc.apache.org/httpclient-3.x/ Commons HttpClient项目现已结束,不再开发.它已被其HttpClient和HttpCore模块中的Apache HttpComponents项目所取代,它们提供更好的性能和更大的灵活性. 从2011年开始,org.apache.commons.httpclient就不再开发.这就是说,它已经落伍了. 方法的对称性上的区别 一.org.apache.http.client org.apache.h

httpclient新旧版本分割点4.3

从这个版本开始,httpclient的api发生了一次重大调整.主要包括如下:Release 4.3 Final ------------------- This is the first stable (GA) release of HttpClient 4.3. The most notable enhancements included in this release are: * Support for Java 7 try-with-resources for resource man

Android之HttpClient的使用方法

HttpClient的使用方法 实例解析:HttpClient的get,post的数据传输 package com.example.httpclient; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URLEncoder; import org.apache.http.HttpEntity; import org.apach

apache.http.client.HttpClient

前言 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活.HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和

org.apache.http.client.HttpClient get/post请求

请求步骤 1.创建httpclient 对象 2.创建 请求方式,构造函数的值为请求路径 3.调用1中对象的execute() 方法,参数为 2 的对象 4.获取请求响应数据 5.释放连接资源 6.处理数据 一.使用org.apache.http.client.HttpClient 的get请求来实现 1.请求核心代码: // 创建 httpclient 对象       HttpClient httpclient = new DefaultHttpClient();    //创建请求方式,因

如何通过httpclient获取访问域名的真实ip

需求:由于目标网站cdn节点很多需要获取到访问当次是哪个ip.1.实现接口HttpConnectionManager新加实现类A,直接复制SimpleHttpConnectionManager的类内容.2.A类增加方法public Socket getSocket() {return this.httpConnection.getSocket();}3.为httpClient指定HttpConnectionManager 方法为 httpClient 指定httpClient.setHttpCo

Java通过httpclient获取cookie模拟登录

package Step1; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpc

org.apache.http.client.HttpClient; HttpClient 4.3超时设置

可用的code public static String doPost(String url, String params, String contentType) /*throws IOException */{ CloseableHttpClient client = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(httpSoTimeout)