Even after doing the below steps, I am unable to establish a connection to https link
Converted .crt and .key file to .p12 through openssl
Converted .p12 file to .jks file through keytool (though I could directly mention .p12 in the code)
- Added generated .jks file to cacerts present in jdk/jre/lib/security
- My client code is here
KeyStore trustStore = KeyStore.getInstance("JKS"); trustStore.load(new FileInputStream("test.jks"), "changeit".toCharArray()); TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(trustStore); TrustManager[] tms = tmf.getTrustManagers(); System.out.println(tms.length); //this is gives me 1 SSLContext sslContext = null; sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, tms, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); URL url; url = new URL(" https://testurl.com"); HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection(); urlConn.setRequestMethod("POST"); urlConn.setRequestProperty("Content-Type", "application/json; utf-8"); urlConn.setRequestProperty("Accept", "application/json"); urlConn.setDoOutput(true); urlConn.setDoInput(true); urlConn.connect();
- Tried adding truststore in System property as well but same error.
- Tried adding truststore/keystore in VM arguments, still got the same error.
What else should I try to make it work?