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();
        }
  • 相关阅读:
    ObjectC&&Swift 渐变色算法实现
    【iOS数据存储】iOS文件系统介绍
    1 、Quartz 2D绘图基础
    iOS 常用框架列表
    【Foundation Frame】Struct
    【Foundation Frame】NSMutableArray
    【Foundation Frame】NSDictionary/NSMutableDictionary
    【Foundation Frame】NSString
    【Foundation Frame】NSArray
    在vue项目中使用自己封装的ajax
  • 原文地址:https://www.cnblogs.com/wjup/p/10576062.html
Copyright © 2011-2022 走看看