How to fix Unirest General SSLEngine problem

H

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:

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:

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:

Unirest.setAsyncHttpClient(createSSLClient());

Hopefully this works for you too. Enjoy.

Add comment

Leave a Reply

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

Tag Cloud

Categories