有没有人通过不做脸TrustManager来解决这个问题????
说真的,所有答案都是“哦,使用假的 TrustManager 并信任所有证书”。
我有一个根 CA 证书,我必须使用它来连接到服务器。
设置 SSLContext 并通过 HttpsURLConnection 使用它非常有效,但是当我尝试将它与 ksoap (HttpsTransportSE) 一起使用时,它让我一直给我 SSLHandshakeException。
我正在使用 ksoap2-android-assembly-3.0.0-jar-with-dependencies.jar,有人知道该版本是否有任何问题吗?
这是我的代码:
CertificateFactory cf = CertificateFactory.getInstance("X.509");
ca = cf.generateCertificate(caInput);
System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
caInput = this.getResources().openRawResource(R.raw.i1);
java.security.cert.Certificate ci1;
ci1 = cf.generateCertificate(caInput);
System.out.println("i1=" + ((X509Certificate) ci1).getSubjectDN());
keyStore.setCertificateEntry("ci1", ci1);
// 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
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
URI u = URI.create("https://myurl/services/myservice");
SoapObject client = new SoapObject("", "dummy");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = client;
HttpsTransportSE ht = new HttpsTransportSE(u.getHost(), u.getPort(), u.getPath(), 10000);
((HttpsServiceConnectionSE) ht.getServiceConnection()).setSSLSocketFactory(context.getSocketFactory());
ht.call("", envelope);
非常感谢任何任何帮助,任何东西......