如何使用android连接启用SSL的asmx Web服务

时间:2015-07-31 09:13:47

标签: android web-services ssl android-ksoap2

我正在尝试连接来自赛门铁克的具有SSL启用证书的asmx Web服务。 Web服务在带有绿色指示的浏览器中正常工作。但无法使用kso​​ap库连接web服务与android。我已将证书(.cer文件)的公钥添加到aseets文件夹中,并将其添加到trustmanager。

1 个答案:

答案 0 :(得分:2)

以下是使用kso​​ap如何通过android和c#webservice处理SSL的完整解决方案。

  1. 像这样自定义你的org.ksoap2.transport.org.ksoap2.transport类构造函数
  2. public HttpTransportSE(Certificate ca, String url) {
        super(ca, url);
    }
    

    2。更改org.ksoap2.transport.ServiceConnectionSE类

    中的一些代码
    public ServiceConnectionSE(Certificate ca, String url) throws IOException
    {
    
        // Create a KeyStore containing our trusted CAs
        String keyStoreType = KeyStore.getDefaultType();
        KeyStore keyStore;
        SSLContext context = null;
        try {
            keyStore = KeyStore.getInstance(keyStoreType);
            keyStore.load(null, null);
            keyStore.setCertificateEntry("ca", ca);
            // Create a TrustManager that trusts the CAs in our KeyStore
            String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
            tmf.init(keyStore);
    
            // Create an SSLContext that uses our TrustManager
            context = SSLContext.getInstance("TLS");
            context.init(null, tmf.getTrustManagers(), null);
    
    
        } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | KeyManagementException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    
        // Tell the URLConnection to use a SocketFactory from our SSLContext
    
    
        connection = (HttpsURLConnection) new URL(url).openConnection();
        connection.setSSLSocketFactory(context.getSocketFactory());
    
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);
    }
    
    1. 并发送您的证书ca
    2. CertificateFactory cf = CertificateFactory.getInstance("X.509");
      AssetManager assetManager = getAssets();
      InputStream caInput = new BufferedInputStream(assetManager.open("your_cert.cer"));
      Certificate ca = cf.generateCertificate(caInput);
      
      AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(ca, url);