美文网首页
HttpClient 4.5.12,修复已弃用的代码

HttpClient 4.5.12,修复已弃用的代码

作者: 一介书生独醉江湖 | 来源:发表于2023-06-14 14:44 被阅读0次
    # 旧版(已废弃)
    SSLSocketFactory ssf = new SSLSocketFactory(context);
                ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                ClientConnectionManager ccm = httpClient.getConnectionManager();
                SchemeRegistry registry = ccm.getSchemeRegistry();
                registry.register(new Scheme("https", 443, ssf));
    
    # 新版
    HttpClientBuilder builder = HttpClientBuilder.create();
                    SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(context, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                    builder.setSSLSocketFactory(sslConnectionFactory);
    
                    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                            .register("https", sslConnectionFactory)
                            .build();
                    HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry);
                    builder.setConnectionManager(ccm);
    
    # 旧版例:
            HttpClient httpClient = HttpClientBuilder.create().build();
            try {
                SSLContext context = SSLContext.getInstance("TLS");
                X509TrustManager tm = new X509TrustManager() {
                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
    
                    @Override
                    public void checkClientTrusted(X509Certificate[] xcs, String str) {
                    }
    
                    @Override
                    public void checkServerTrusted(X509Certificate[] xcs, String str) {
                    }
                };
                context.init(null, new TrustManager[]{tm}, null);
    
                SSLSocketFactory ssf = new SSLSocketFactory(context);
                ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                ClientConnectionManager ccm = httpClient.getConnectionManager();
                SchemeRegistry registry = ccm.getSchemeRegistry();
                registry.register(new Scheme("https", 443, ssf));
            } catch (KeyManagementException ex) {
                throw new RuntimeException(ex);
            } catch (NoSuchAlgorithmException ex) {
                throw new RuntimeException(ex);
            }                
            return httpClient;
    
    # 新版例
                try {
                    SSLContext context = SSLContext.getInstance("TLS");
                    X509TrustManager tm = new X509TrustManager() {
                        @Override
                        public X509Certificate[] getAcceptedIssuers() {
                            return null;
                        }
    
                        @Override
                        public void checkClientTrusted(X509Certificate[] xcs, String str) {
                        }
                        @Override
                        public void checkServerTrusted(X509Certificate[] xcs, String str) {
                        }
                    };
                    context.init(null, new TrustManager[]{tm}, null);
    
                    HttpClientBuilder builder = HttpClientBuilder.create();
                    SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(context, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                    builder.setSSLSocketFactory(sslConnectionFactory);
    
                    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                            .register("https", sslConnectionFactory)
                            .build();
                    HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry);
                    builder.setConnectionManager(ccm);
                    return builder.build();
                } catch (Exception e) {
                    logger.error("sslClient异常: {}", e.getMessage());
                    e.printStackTrace();
                }
    

    相关文章

      网友评论

          本文标题:HttpClient 4.5.12,修复已弃用的代码

          本文链接:https://www.haomeiwen.com/subject/iztkydtx.html