美文网首页高级UI
Glide加载hppts图片失败的解决办法,亲测有效

Glide加载hppts图片失败的解决办法,亲测有效

作者: 程序员大耳 | 来源:发表于2020-03-06 11:36 被阅读0次

    Glide加载https图片报错:javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

    解决办法:在Application的onCreate中调用方法handleSSLHandshake()

    /**

        * 忽略https的证书校验

        * 避免Glide加载https图片报错:

        * javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

    java.security.cert。X509Certificate;

        */

        public static void handleSSLHandshake() {

            try {

                TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {

                    public X509Certificate[] getAcceptedIssuers() {

                        return new X509Certificate[0];

                    }

                    @Override

                    public void checkClientTrusted(X509Certificate[] certs, String authType) {

                    }

                    @Override

                    public void checkServerTrusted(X509Certificate[] certs, String authType) {

                    }

                }};

                SSLContext sc = SSLContext.getInstance("TLS");

                // trustAllCerts信任所有的证书

                sc.init(null, trustAllCerts, new SecureRandom());

                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

                HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

                    @Override

                    public boolean verify(String hostname, SSLSession session) {

                        return true;

                    }

                });

            } catch (Exception ignored) {

            }

        }

    相关文章

      网友评论

        本文标题:Glide加载hppts图片失败的解决办法,亲测有效

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