【问题标题】:How to connect an SSL enabled asmx web service with android如何将启用 SSL 的 asmx Web 服务与 android 连接
【发布时间】:2026-01-23 23:15:02
【问题描述】:

我正在尝试连接一个 asmx Web 服务,该服务具有来自 symantec 的启用 SSL 的证书。 Web 服务在带有绿色指示的浏览器中运行良好。但无法使用 ksoap 库将 web 服务与 android 连接。我已经在aseets文件夹中添加了证书(.cer文件)的公钥并将其添加到信任管理器中。

【问题讨论】:

  • 前段时间我遇到了同样的问题,最后我使用普通的 java HTTP 连接调用了 web 服务。
  • 是的,在后台终于 http url 连接工作了……看看如何使用 http url 连接调用soap webservice。 *.com/questions/31286722/…
  • 但是使用 https 是受 SSL 保护的。 http 没有提供客户端和服务器之间的安全路径。
  • 请使用 HttpsUrlConnection。
  • @NaveenRamawat 让我试试。谢谢。

标签: android web-services ssl android-ksoap2


【解决方案1】:

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

  1. 像这样自定义您的 org.ksoap2.transport.org.ksoap2.transport 类构造函数

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

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

【讨论】: