Created
August 4, 2016 12:20
-
-
Save robinraju/f89377edd5aba90f5105ff8f195b286a to your computer and use it in GitHub Desktop.
Ignoring SSL certificate – javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.net.ssl.HttpsURLConnection; | |
import javax.net.ssl.SSLContext; | |
import javax.net.ssl.TrustManager; | |
import javax.net.ssl.X509TrustManager; | |
import java.security.SecureRandom; | |
import java.security.cert.X509Certificate; | |
public class ConnectionUtil { | |
public static void trustConnection(){ | |
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){ | |
public X509Certificate[] getAcceptedIssuers(){return null;} | |
public void checkClientTrusted(X509Certificate[] certs, String authType){} | |
public void checkServerTrusted(X509Certificate[] certs, String authType){} | |
}}; | |
try { | |
SSLContext sc = SSLContext.getInstance("TLS"); | |
sc.init(null, trustAllCerts, new SecureRandom()); | |
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment