Skip to content

Instantly share code, notes, and snippets.

@timothyasp
Created September 5, 2013 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timothyasp/6455850 to your computer and use it in GitHub Desktop.
Save timothyasp/6455850 to your computer and use it in GitHub Desktop.
final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
public static OkHttpClient createOkHttpClient() {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException { }
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException { }
} };
OkHttpClient client = new OkHttpClient();
SSLContext sslContext;
try {
sslContext = SSLContext.getInstance("TLS");
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
else
sslContext.init(null, null, null);
} catch (GeneralSecurityException e) {
throw new AssertionError(); // The system has no TLS. Just give up.
}
client.setSslSocketFactory(sslContext.getSocketFactory());
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)
client.setHostnameVerifier(DO_NOT_VERIFY);
return client;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment