KSOAP 2 Android with HTTPS

后端 未结 4 1331
离开以前
离开以前 2020-11-28 09:11

I am using KSOAP2 to manage SOAP in Android but it use https for the SOAP url and I am getting this error: javax.net.ssl.SSLException: Not trusted server certificate
A n

4条回答
  •  误落风尘
    2020-11-28 09:35

    Create a new class FakeX509TrustManager to handle the certificate problem,

        FakeX509TrustManager.allowAllSSL();
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    

    The new created class is as the following:

    public class FakeX509TrustManager implements X509TrustManager { 
    
        private static TrustManager[] trustManagers; 
        private static final X509Certificate[] _AcceptedIssuers = new 
    X509Certificate[] {}; 
    
        @Override 
        public void checkClientTrusted(X509Certificate[] chain, String 
    authType) throws CertificateException { 
        } 
    
        @Override 
        public void checkServerTrusted(X509Certificate[] chain, String 
    authType) throws CertificateException { 
        } 
    
        public boolean isClientTrusted(X509Certificate[] chain) { 
                return true; 
        } 
    
        public boolean isServerTrusted(X509Certificate[] chain) { 
                return true; 
        } 
    
        @Override 
        public X509Certificate[] getAcceptedIssuers() { 
                return _AcceptedIssuers; 
        } 
    
        public static void allowAllSSL() { 
                HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() 
    { 
                        @Override 
                        public boolean verify(String hostname, SSLSession session) { 
                                return true; 
                        } 
    
                }); 
    
                SSLContext context = null; 
                if (trustManagers == null) { 
                        trustManagers = new TrustManager[] { new FakeX509TrustManager() }; 
                } 
    
                try { 
                        context = SSLContext.getInstance("TLS"); 
                        context.init(null, trustManagers, new SecureRandom()); 
                } catch (NoSuchAlgorithmException e) { 
                        e.printStackTrace(); 
                } catch (KeyManagementException e) { 
                        e.printStackTrace(); 
                } 
    
           HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory()); 
        } 
    
    } 
    

提交回复
热议问题