Https 忽略证书\使用自定义证书的java代码实现

时间:2022-09-03 03:23:25
public SSLContext createIgnoreVerifySSL() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {

        SSLContext sc = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {

            public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
} }).build(); return sc;
} protected HttpClient getHttpClient() throws Exception { HttpClientBuilder builder = HttpClientBuilder.create();
SSLConnectionSocketFactory sslConnSocketFactory = new SSLConnectionSocketFactory(createIgnoreVerifySSL());
builder.setSSLSocketFactory(sslConnSocketFactory); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create().register("https", sslConnSocketFactory).build(); HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry); builder.setConnectionManager(ccm); return builder.build(); }