Created
August 25, 2015 10:26
-
-
Save barbietunnie/26e21b70f2986c781f10 to your computer and use it in GitHub Desktop.
This file contains 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
public void doTrustToCertificates() throws Exception { | |
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); | |
TrustManager[] trustAllCerts = new TrustManager[]{ | |
new X509TrustManager() { | |
public X509Certificate[] getAcceptedIssuers() { | |
return null; | |
} | |
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException { | |
return; | |
} | |
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException { | |
return; | |
} | |
} | |
}; | |
SSLContext sc = SSLContext.getInstance("SSL"); | |
sc.init(null, trustAllCerts, new SecureRandom()); | |
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); | |
HostnameVerifier hv = new HostnameVerifier() { | |
public boolean verify(String urlHostName, SSLSession session) { | |
logger.info("+++ URL host name: " + urlHostName + ", peer host: " + session.getPeerHost() + " +++"); | |
if (!urlHostName.equalsIgnoreCase(session.getPeerHost())) { | |
System.out.println("Warning: URL host '" + urlHostName + "' is different to SSLSession host '" + session.getPeerHost() + "'."); | |
} | |
return true; | |
} | |
}; | |
HttpsURLConnection.setDefaultHostnameVerifier(hv); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment