Android 信任https证书


    public static void handleSSLHandshake() {
    
    
        try {
    
    
            TrustManager[] trustAllCerts = new TrustManager[]{
    
    new 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];
                }
            }};
            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) {
    
    

        }
    }

猜你喜欢

转载自blog.csdn.net/Chen_xiaobao/article/details/125970702