Trusting all certificates using HttpClient over HTTPS

后端 未结 21 2236
北恋
北恋 2020-11-21 04:50

Recently posted a question regarding the HttpClient over Https (found here). I\'ve made some headway, but I\'ve run into new issues. As with my last problem, I

21条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 05:28

    The API of HttpComponents has got changed. It works with the code below.

    public static HttpClient getTestHttpClient() {
        try {
            SSLSocketFactory sf = new SSLSocketFactory(new TrustStrategy(){
                @Override
                public boolean isTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                    return true;
                }
            }, new AllowAllHostnameVerifier());
    
            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("https",8444, sf));
            ClientConnectionManager ccm = new ThreadSafeClientConnManager(registry);
            return new DefaultHttpClient(ccm);
        } catch (Exception e) {
            e.printStackTrace();
            return new DefaultHttpClient();
        }
    }
    

提交回复
热议问题