undefinedfix
Sign in

Java calls the webapi of HTTPS through httpclient

tblev edited in Sat, 09 Jan 2021

How can Java call webapi in the form of HTTPS through httpclient? How to verify a certificate. Example: https://devdata.osisoft.com/p... The interface needs to be called through httpclient. I haven't done the call in the way of HTTPS. I don't know how to solve it.

1 Replies
Amer
commented on Sat, 09 Jan 2021

Our company's practice is to ignore the certificate verification, and the code is as follows:

 SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                // 信任所有
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            }).build();
            HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create().register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", sslsf).build();
            cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
            cm.setMaxTotal(maxTotalConn);
            cm.setDefaultMaxPerRoute(maxPerRoute);
            httpClient = HttpClients.custom().setConnectionManager(cm).setSSLSocketFactory(sslsf).build();
lock This question has been locked and the reply function has been disabled.