zoukankan      html  css  js  c++  java
  • 转载-解决使用httpClient 4.3.x登陆 https时的证书报错问题

    今天在使用httpClient4.3.6模拟登陆https网站的时候出现了证书报错的问题,这是在开源中国社区里找到的可行的答案(原文链接:http://www.oschina.net/question/194048_135225?sort=time)

    大概逻辑是自己实现了信任所有证书

    import javax.net.ssl.SSLContext;
     
    import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
    import org.apache.http.conn.ssl.SSLContextBuilder;
    import org.apache.http.conn.ssl.TrustStrategy;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    /**
     * 
     * @author Sayi
     *
     */
    public abstract class SSLUtils {
     
        public static CloseableHttpClient createSSLInsecureClient() {
            try {
                SSLContext sslContext = new SSLContextBuilder()
                                    .loadTrustMaterial(null, new TrustStrategy() {
                    //信任所有
                    public boolean isTrusted(X509Certificate[] chain,
                                    String authType) throws CertificateException {
                        return true;
                    }
                        }).build();
                SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                        sslContext);
                return HttpClients.custom().setSSLSocketFactory(sslsf).build();
            } catch (KeyManagementException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (KeyStoreException e) {
                e.printStackTrace();
            }
            return  HttpClients.createDefault();
        }
     
    }
  • 相关阅读:
    Python模块之pysnooper
    本站页脚HTML回顶部代码
    本站CSS代码
    Linux使用 tar命令-g参数进行增量+差异备份、还原文件
    mysql定时备份shell脚本
    Linux系统备份与还原
    MYSQL备份与恢复
    技术普及帖:你刚才在淘宝上买了一件东西
    Linux运维工程师前景
    Linux运维工程师需掌握的技能
  • 原文地址:https://www.cnblogs.com/jiaoyiping/p/4189519.html
Copyright © 2011-2022 走看看