Forked from drakeet/OkHttp with https getUnsafeOkHttpClient
Created
November 22, 2018 07:53
-
-
Save flyfire/d6cd840d7703f8adfc921727ace9f790 to your computer and use it in GitHub Desktop.
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
| import com.squareup.okhttp.OkHttpClient; | |
| import java.security.cert.CertificateException; | |
| import javax.net.ssl.HostnameVerifier; | |
| import javax.net.ssl.SSLContext; | |
| import javax.net.ssl.SSLSession; | |
| import javax.net.ssl.SSLSocketFactory; | |
| import javax.net.ssl.TrustManager; | |
| import javax.net.ssl.X509TrustManager; | |
| public class OkHttpUtils { | |
| public static OkHttpClient getUnsafeOkHttpClient() { | |
| try { | |
| 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 null; | |
| } | |
| } | |
| }; | |
| final SSLContext sslContext = SSLContext.getInstance("SSL"); | |
| sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); | |
| final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); | |
| OkHttpClient okHttpClient = new OkHttpClient(); | |
| okHttpClient.setSslSocketFactory(sslSocketFactory); | |
| okHttpClient.setHostnameVerifier(new HostnameVerifier() { | |
| @Override public boolean verify(String hostname, SSLSession session) { | |
| return true; | |
| } | |
| }); | |
| return okHttpClient; | |
| } 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