-
-
Save mefarazath/c9b588044d6bffd26aac3c520660bf40 to your computer and use it in GitHub Desktop.
Get OkHttpClient which ignores all SSL errors.
This file contains hidden or 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
private static OkHttpClient getUnsafeOkHttpClient() { | |
try { | |
// 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) throws CertificateException { | |
} | |
@Override | |
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, | |
String authType) throws CertificateException { | |
} | |
@Override | |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { | |
return new X509Certificate[0]; | |
} | |
} | |
}; | |
// 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(); | |
return new OkHttpClient.Builder() | |
.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]) | |
.hostnameVerifier(new HostnameVerifier() { | |
@Override | |
public boolean verify(String hostname, SSLSession session) { | |
return true; | |
} | |
}).build(); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} |
What's a difference between this approach and standard Okhttp builder methods?
final OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder() .sslSocketFactory(SslMockUtils.TRUST_ALL_SOCKET_FACTORY, SslMockUtils.TRUST_MANAGER) .hostnameVerifier(SslMockUtils.TRUST_ALL_HOSTNAME_VERIFIER);
SslMockUtils is Not standart for OkHttpClient . Maybe this Utils have same realisation
Is there a way to write this in version okhttp 2.7.5? OkHttpClient
does not have a Builder
in that version.
I replaced the return statement with these lines:
OkHttpClient client = new OkHttpClient();
client.setSocketFactory(sslSocketFactory);
client.setHostnameVerifier(
new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
);
return client;
It seems like OkHttpClient
does not have aX509TrustManager
field in version 2.7.5, so I omitted it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's a difference between this approach and standard Okhttp builder methods?