Skip to content

Instantly share code, notes, and snippets.

@barlog-m
Last active December 8, 2015 09:27
Show Gist options
  • Save barlog-m/4f98d7e64fdfadaca21a to your computer and use it in GitHub Desktop.
Save barlog-m/4f98d7e64fdfadaca21a to your computer and use it in GitHub Desktop.
Ignore self-signed SSL certificates
private void trustAllCertificates() {
try {
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
} catch (Exception e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment