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

Posted Blade blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Https 忽略证书使用自定义证书的java代码实现相关的知识,希望对你有一定的参考价值。

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();

    }

 

以上是关于Https 忽略证书使用自定义证书的java代码实现的主要内容,如果未能解决你的问题,请参考以下文章

iOS https请求对自签名证书忽略

使用 Java 忽略 SSL 证书错误

在Android应用中使用自定义证书的HTTPS连接(下)

Jetty Websocket 连接 - 忽略自签名证书

c# https请求忽略证书验证_各种编程语言忽略http的SSL证书认证

Python 发送带自签名证书的 https 请求