【问题标题】:Android web-services using KSOAP2 and HTTPS使用 KSOAP2 和 HTTPS 的 Android 网络服务
【发布时间】:2011-07-14 09:27:24
【问题描述】:

我正在尝试使用 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 Connection Android

4 - Apache HttpClient on Android producing CertPathValidatorException (IssuerName != SubjectName)

但不能直接使用。

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

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

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

【问题讨论】:

    标签: android web-services ssl android-ksoap2


    【解决方案1】:

    为我工作 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 {
    
            }
        }
    

    【讨论】:

    • 我有客户端证书和 SSL 调用,这种方法仍然有效吗?
    • 很遗憾,时间太长了。我不知道
    猜你喜欢
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多