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

How to connect an SSL enabled asmx web service with android

我正在尝试连接一个 asmx Web 服务,该服务具有来自赛门铁克的支持 SSL 的证书。 Web 服务在浏览器中运行良好,并带有绿色指示。但是无法使用 ksoap 库将 Web 服务与 android 连接。我已经在 aseets 文件夹中添加了证书(.cer 文件)的 public 密钥并将其添加到 trustmanager。

这里是关于如何使用 ksoap 通过 android 和 c# webservice 处理 SSL 的完整解决方案。

  1. 自定义您的组织。ksoap2.transport.org.ksoap2.transport class 像这样的构造函数

public HttpTransportSE(Certificate ca, String url) {
    super(ca, url);
}

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

中的一些代码
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

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);