美文网首页
Https & OkHttpClient

Https & OkHttpClient

作者: lotusve_w | 来源:发表于2019-05-23 09:57 被阅读0次
       OkHttpClient client = new OkHttpClient.Builder()
               .sslSocketFactory(getSSLContext().getSocketFactory(), new AllTrustManager())
               .build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                /*.client(client)*/
                .build();
private SSLContext getSSLContext(){
        SSLContext sslContext = null;
        try {
            sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[]{new AllTrustManager()}, new SecureRandom());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sslContext;
    }
private class AllTrustManager implements X509TrustManager {

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    }

相关文章

网友评论

      本文标题:Https & OkHttpClient

      本文链接:https://www.haomeiwen.com/subject/fzomzqtx.html