HttpClient 发起不校验 Https CA证书的 Post 请求

最近,在对接网联联调,采用IP作为请求地址,但采用HTTPS形式。证书校验不通过,添加证书到本地又不合理,根据网上的例子,写了一个测试类:

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class NoSSLHttpsTest {
    public static void main(String[] args) throws Exception {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
            public void checkClientTrusted(X509Certificate[] arg0,
                                           String arg1) throws CertificateException {
            }
            public void checkServerTrusted(X509Certificate[] arg0,
                                           String arg1) throws CertificateException {
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(ctx, NoopHostnameVerifier.INSTANCE);
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(ssf).build();

        /**
         * 以下写发起请求的部分
         */

    }
}
0

发表评论

This site uses Akismet to reduce spam. Learn how your comment data is processed.