zoukankan      html  css  js  c++  java
  • CloseableHttpClient获取https请求不验证证书

    创建---调用

    CloseableHttpClient httpclient = getHttpsClient();
    /**
         * 获取https连接(不验证证书)
         *
         * @return
         */
        private static CloseableHttpClient getHttpsClient() {
            RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.<ConnectionSocketFactory>create();
            ConnectionSocketFactory plainSF = new PlainConnectionSocketFactory();
            registryBuilder.register("http", plainSF);
            // 指定信任密钥存储对象和连接套接字工厂
            try {
                KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
                // 信任任何链接
                TrustStrategy anyTrustStrategy = new TrustStrategy() {
    
                    @Override
                    public boolean isTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
                        // TODO Auto-generated method stub
                        return true;
                    }
                };
                SSLContext sslContext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, anyTrustStrategy).build();
                LayeredConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                registryBuilder.register("https", sslSF);
            } catch (KeyStoreException e) {
                throw new RuntimeException(e);
            } catch (KeyManagementException e) {
                throw new RuntimeException(e);
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(e);
            }
            Registry<ConnectionSocketFactory> registry = registryBuilder.build();
            // 设置连接管理器
            PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);
            // 构建客户端
            return HttpClientBuilder.create().setConnectionManager(connManager).build();
        }
  • 相关阅读:
    抓包
    tk(三)按钮的事件绑定
    python xlrd 模块(获取Excel表中数据)
    使用pycharm搜索框和正则表达式匹配内容
    Progressbar 实例
    python获取时间
    excel用xlrd日期变成42631.0
    Python中super的用法【转载】
    python类的继承和多态
    均值的性质及其应用
  • 原文地址:https://www.cnblogs.com/wjup/p/10576062.html
Copyright © 2011-2022 走看看