okHttp3.0直接访问https javax.net.ssl.SSLHandshakeException报错问题,忽略https证书

//okHttp3添加信任所有证书

public static OkHttpClient getUnsafeOkHttpClient() {

    try {

        final TrustManager[] trustAllCerts = new TrustManager[]{

                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[]{};

                    }

                }

        };

        final SSLContext sslContext = SSLContext.getInstance("SSL");

        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

        final javax.net.ssl.SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

        OkHttpClient.Builder builder = new OkHttpClient.Builder();

        builder.sslSocketFactory(sslSocketFactory);

        builder.hostnameVerifier(new HostnameVerifier() {

            @Override

            public boolean verify(String hostname, SSLSession session) {

                return true;

            }

        });

        return builder.build();

    } catch (Exception e) {

        throw new RuntimeException(e);

    }

}

初始化的时候替换:

OkHttpClient okHttpClient = new OkHttpClient();

为:

OkHttpClient okHttpClient = getUnsafeOkHttpClient();

你可能感兴趣的:(okHttp3.0直接访问https javax.net.ssl.SSLHandshakeException报错问题,忽略https证书)