I tried to post data to a rest service using Unirest (unirest.io). But when I tried to post to an HTTPS url, I got the following error:
1 2 3 |
javax.net.ssl.SSLHandshakeException: General SSLEngine problem PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target |
To fix this, you could use the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
private CloseableHttpAsyncClient createSSLClient() { TrustStrategy acceptingTrustStrategy = new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { return true; } }; SSLContext sslContext = null; try { sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); } catch (Exception e) { LOGGER.error("Could not create SSLContext"); } return HttpAsyncClients.custom() .setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER) .setSSLContext(sslContext).build(); } |
Then, you need to execute the code below before using Unirest itself:
1 |
Unirest.setAsyncHttpClient(createSSLClient()); |
Hopefully this works for you too. Enjoy