I am trying to create a org.apache.http.impl.client.CloseableHttpClient that trusts self signed certificates.
Here is the code I have tried:
private void makeCall() {
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setSSLContext(
new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build()
)
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
HttpGet httpget = new HttpGet("https://selfSignedCertSite.somedomain.com/");
try {
try (CloseableHttpResponse response = httpClient.execute(httpget)) {
System.out.println(response.toString());
}
}catch(Exception e){
//PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
System.out.println(e.getMessage());
}
}
I still get an exception with the following message:
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
I do not want to change JKS files as my app is using all default ones and I want to configure it using code only.
How do I do it?