我正在尝试连接来自赛门铁克的具有SSL启用证书的asmx Web服务。 Web服务在带有绿色指示的浏览器中正常工作。但无法使用ksoap库连接web服务与android。我已将证书(.cer文件)的公钥添加到aseets文件夹中,并将其添加到trustmanager。
答案 0 :(得分:2)
以下是使用ksoap如何通过android和c#webservice处理SSL的完整解决方案。
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);
}
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);