Not trusted certificate using ksoap2-android

前端 未结 4 1485
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 02:57

I\'m using ksoap2-android to make a call to wcf service over SSL. I can get it to work without SSL, but now I want to make the call over SSL, but I\'ve run in to some proble

4条回答
  •  借酒劲吻你
    2020-12-06 03:59

    To complement the answer of Vedran with some source code, sorry I can't comment.

    The trustManager:

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

    The call at your method:

    allowAllSSL();
    HttpsTransportSE httpsTransport = new HttpsTransportSE(Server,443, URL, 1000);
    

    Notes:

    1. Server is the server url.
    2. 443 is the default https port, you still have to specify a port since the constructor expects one.
    3. URL the path to the WS operation
    4. 1000 es the timeout

    Which is constructed as: [https://Server:443/URL]

提交回复
热议问题