4

我正在尝试使用 JAVA 和 KSOAP2 为 android 编写 web 服务。SOAP 是我可以使用的唯一协议,而 ReST 不是一个选项。

因此,我成功创建了 SOAP 请求并使用 HTTP 连接到服务器。但是,我需要 HTTPS,因为敏感信息会被传输。禁用证书检查不是一个选项,因为数据很敏感,我必须使用 SSL。

由于 Android 在 HTTPS 中引发了认证错误,因此我创建了自己的密钥库

1- http://blog.antoine.li/index.php/2010/10/android-trusting-ssl-certificates/

并将其添加到项目中。

我的代码类似于
2- http://www.techques.com/question/1-4646121/Not-trusted-certificate-using-ksoap2-android

我也经历过

3 - HTTPS 连接安卓

4 - Android 上的 Apache HttpClient 产生 CertPathValidatorException (IssuerName != SubjectName)

但不能直接使用它们。

非常感谢使用 HTTPS 显示 1 中的代码与 2 中的代码相关的伪代码。

2中的最后一条评论实际上是什么意思?他在代码中使用了 HttpsTransportSE,但表示他扩展了 HttpsServiceConnectionSE。你能在伪代码中显示这个吗?

另外,我应该使用 HttpsTransportSE 还是 HttpsServiceConnectionSE 来提供我将要连接的 URL。

4

1 回答 1

0

为我工作 KSOAP + Web 服务 WCF 与 Eclipse。带有证书和登录名/密码的 Tomcat

也许这可以帮助你

private static SoapObject getBody(final SoapSerializationEnvelope soapEnvelope) throws Exception {
        if (soapEnvelope.bodyIn == null) {
            throw new Exception("soapEnvelope.bodyIn=null");
        }
        else if (soapEnvelope.bodyIn.getClass() == SoapFault.class) {
            throw new ExceptionLogic((SoapFault) soapEnvelope.bodyIn));
        }
        else {
            return (SoapObject) soapEnvelope.bodyIn;
        }

    }

private static SoapSerializationEnvelope sendRequete(final SoapObject soapReq, final String classMappingName,
            final Class<?> classMapping, final int timeOutSpecial) {



        final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.implicitTypes = true;
        soapEnvelope.dotNet = true;

        if (classMappingName != null) {
            soapEnvelope.addMapping(NAMESPACE, classMappingName, classMapping);
        }

        soapEnvelope.setOutputSoapObject(soapReq);

        try {

            final HttpTransportSE httpTransport = new HttpTransportSE(Constante.urlWebService, timeOutSpecial);
            httpTransport.debug = BuildConfig.DEBUG;

            // Prod
            if (Constante.urlWebService.startsWith("https://")) {
                final List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
                headerList.add(new HeaderProperty("Authorization", "Basic "
                        + org.kobjects.base64.Base64.encode((Constante.CERTIFICAT_LOGIN + ":" + Constante.CERTIFICAT_MDP).getBytes())));

                FakeX509TrustManager.allowAllSSL();
                httpTransport.call(NAMESPACE + "/" + soapReq.getName(), soapEnvelope, headerList);
            }
            // Test
            else {
                httpTransport.call(NAMESPACE + "/" + soapReq.getName(), soapEnvelope);
            }

            return soapEnvelope;
        }
        catch (final Exception e) {
            throw new Exception("Erreur : " + e.getMessage(), e);
        }

    }



    private static class FakeX509TrustManager implements X509TrustManager {
        private static TrustManager[] trustManagers;
        private final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return _AcceptedIssuers;
        }

        public static void allowAllSSL() {
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

                @Override
                public boolean verify(final String hostname, final SSLSession session) {
                    return true;
                }
            });
            SSLContext context = null;
            if (trustManagers == null) {
                trustManagers = new TrustManager[] { new FakeX509TrustManager() };
            }
            try {
                context = SSLContext.getInstance("TLS");
                context.init(null, trustManagers, new SecureRandom());
            }
            catch (final NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            catch (final KeyManagementException e) {
                e.printStackTrace();
            }
            HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
        }

        @Override
        public void checkClientTrusted(final X509Certificate[] arg0, final String arg1) throws CertificateException {

        }

        @Override
        public void checkServerTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {

        }
    }
于 2013-04-11T14:13:30.540 回答