Created
October 19, 2017 08:21
-
-
Save swissonid/b7925a2afc4e85e0bd610911b81ef71e to your computer and use it in GitHub Desktop.
OkHttp accept all SSL Cetfication
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
class IgnoreSSL { | |
static X509TrustManager doNotValidateTrustManager() { | |
// Create a trust manager that does not validate certificate chains | |
return new X509TrustManager() { | |
@Override | |
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) { | |
} | |
@Override | |
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) { | |
} | |
@Override | |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { | |
return new java.security.cert.X509Certificate[0]; | |
} | |
}; | |
} | |
static SSLSocketFactory getSSLSocketFactory() { | |
try { | |
// Install the all-trusting trust manager | |
TrustManager[] trustManagers = {doNotValidateTrustManager()}; | |
final SSLContext sslContext = SSLContext.getInstance("SSL"); | |
sslContext.init(null, trustManagers, new java.security.SecureRandom()); | |
// Create an ssl socket factory with our all-trusting manager | |
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); | |
return sslSocketFactory; | |
}catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment