ksoap2 HttpsTransportSE:不使用证书

时间:2013-06-14 20:51:00

标签: android https ksoap

有没有人通过不面对TrustManager来解决这个问题?

说真的,所有答案都是“哦使用假的TrustManager并信任所有证书”。

我有一个必须用来连接服务器的根CA证书。

设置SSLContext并通过HttpsURLConnection使用它可以很好地工作,但是当我尝试将它与ksoap(HttpsTransportSE)一起使用时,它会让我给我SSLHandshakeException。

我正在使用kso​​ap2-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);

非常感谢任何帮助,任何帮助......

0 个答案:

没有答案