美文网首页
Retrofit+OKHttp 忽略 https 证书验证

Retrofit+OKHttp 忽略 https 证书验证

作者: 蜂鸟之歌 | 来源:发表于2019-11-27 22:13 被阅读0次

    开发或者测试过程种有时需要忽略 https 证书验证,通过 OKHttpClient.Builder 对象调用 “sslSocketFactory ”方法 和 “hostnameVerifier ” 方法。

    builder.sslSocketFactory(SSLSocketClient.getSSLSocketFactory());
    builder.hostnameVerifier(SSLSocketClient.getHostnameVerifier());
    
    public class SSLSocketClient
    {
        //获取这个SSLSocketFactory  
        public static SSLSocketFactory getSSLSocketFactory()
        {
            try
            {
                SSLContext sslContext = SSLContext.getInstance("SSL");
                sslContext.init(null, getTrustManager(), new SecureRandom());
                return sslContext.getSocketFactory();
            }
            catch (Exception e)
            {
                throw new RuntimeException(e);
            }
        }
     
        //获取TrustManager  
        private static TrustManager[] getTrustManager()
        {
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager()
            {
                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType)
                {
                }
     
                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType)
                {
                }
     
                @Override
                public X509Certificate[] getAcceptedIssuers()
                {
                    return new X509Certificate[]{};
                }
            }};
            return trustAllCerts;
        }
     
        //获取HostnameVerifier  
        public static HostnameVerifier getHostnameVerifier()
        {
            HostnameVerifier hostnameVerifier = new HostnameVerifier()
            {
                @Override
                public boolean verify(String s, SSLSession sslSession)
                {
                    return true;
                }
            };
            return hostnameVerifier;
        }
    }
    

    相关文章

      网友评论

          本文标题:Retrofit+OKHttp 忽略 https 证书验证

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