Skip to content

Instantly share code, notes, and snippets.

@zukka77
Last active January 10, 2016 17:23
Show Gist options
  • Save zukka77/54df6701fecf21452fa7 to your computer and use it in GitHub Desktop.
Save zukka77/54df6701fecf21452fa7 to your computer and use it in GitHub Desktop.
Unsafe OkHttpClient
private static OkHttpClient getUnsafeOkHttpClient() throws KeyManagementException, NoSuchAlgorithmException {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
Arrays.stream(chain).forEach((c)->System.out.println("Issuer: "+c.getIssuerDN()));
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient client = new OkHttpClient();
client.setSslSocketFactory(sslSocketFactory);
client.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
return client;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment