• 新旧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     }
  • 相关阅读:
    k8s 新加节点
    /etc/bashrc
    k8s 连接harbor 的私有仓库的两种方法 一种是secret 绑定到sa serviceaccount 账号下 一种是需要绑定到 imagePullSecrets:
    pip install --upgrade urllib3==1.25.2
    mysql skip-grant-tables 后要多次重启 和验证登录检查确认密码生效
    k8s 传参给docker env command、args和dockerfile中的entrypoint、cmd之间的关系
    kubectl -n ingress-nginx exec nginx-ingress-controller-78bd49949c-t22bl -- cat /etc/nginx/nginx.conf
    更新Alpine Linux源 sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories apk add xxx
    ingress nginx https配置
    Ingress-nginx 部署使用
  • 原文地址:https://www.cnblogs.com/snowater/p/7591128.html
Copyright © 2020-2023  润新知