How to bypass trusted host and certificate check in Java
You probably debug your secure app and have no money for getting certificate yet
I decided to share my experience, since nothing can be easily found on net, especially if you use crappy Google. Just call method below prior of using HTTPUrlConnection to connect to not trusted server when you're getting exceptions like: java.security.cert.CertificateException: Could not find trusted certificate
public static void installAllTrustManager() {
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
try {
sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(
new HostnameVerifier() {
public boolean verify(String urlHostname, javax.net.ssl.SSLSession _session) {
return true;
}
}
);
} catch (Exception e) {
e.printStackTrace();
}
}