-
-
Save SteinerOkay/7537184 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
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