2
votes
CloseableHttpClient client = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();

is giving me an error of :

[sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target]

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

i.e. it is not adding all self-signed certificates as expected..

My HTTPClient version is 4.5.1 and HTTPCore is version 4.4.4

Please give me a solution without using deprecated methods like SSLContextBuilder

1

1 Answers

5
votes

Try using this snippet:

import java.security.*;
import org.apache.http.conn.ssl.*;


try
{
  SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy() {
    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException
    {
      return true;
    }
  }).build();

  CloseableHttpClient client =HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier())
                              .build();
 }
 catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e)
 {
   e.printStackTrace();
 }