Android ignore self signed certificate

后端 未结 1 1451
不知归路
不知归路 2021-01-01 07:10

My Android App is connecting to https self-signed server & it is working fine with using client certificates (.cer file).

Can Android App be connected to https

相关标签:
1条回答
  • 2021-01-01 08:04

    You can use Volley Android library for that.

    You will need to add the below code on the onCreate() of your application class. By overwriting the X509Certificate basically you will accept all certificates.

    try {
            TrustManager[] victimizedManager = new TrustManager[]{
    
                    new X509TrustManager() {
    
                        public X509Certificate[] getAcceptedIssuers() {
    
                            X509Certificate[] myTrustedAnchors = new X509Certificate[0];
    
                            return myTrustedAnchors;
                        }
    
                        @Override
                        public void checkClientTrusted(X509Certificate[] certs, String authType) {
                        }
    
                        @Override
                        public void checkServerTrusted(X509Certificate[] certs, String authType) {
                        }
                    }
            };
    
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, victimizedManager, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String s, SSLSession sslSession) {
                    return true;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    0 讨论(0)
提交回复
热议问题