KSOAP 2的Andr​​oid使用HTTPS [英] KSOAP 2 Android with HTTPS

查看:305
本文介绍了KSOAP 2的Andr​​oid使用HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用KSOAP2在Android的管理SOAP,但它使用https的SOAP URL和我收到此错误:javax.net.ssl​​.SSLException:不被信任的服务器证书
一个正常的错误,因为该证书是不可信的,但任何人都知道如何要解决这个错误? 因为是从其他的公司我不能管理证书,我没有机会去改变它。

I am using KSOAP2 to manage SOAP in Android but it use https for the SOAP url and I am getting this error: javax.net.ssl.SSLException: Not trusted server certificate
A normal error because the certificate is untrusted, but anyone knows how to workaround with this error? I can not manage the certificate because is from a other company and I don't have access to change it.

感谢

推荐答案

我不能评论还没有,所以我发表我的评论rallat回答在这里。他的解决方案工作,但还需要进一步的解释。使用SSL运行ksoap2:

I can't comment yet so i post my comments to rallat answer here. His solution works but it needs further explanations. To run ksoap2 with ssl:

  1. ksoap2-机器人组装-2.5.2-JAR-与-dependencies.jar在项目
  2. 在下载ksoap2源从<一个href="https://github.com/mosabua/ksoap2-android/tree/">https://github.com/mosabua/ksoap2-android/tree/ (ksoap2库)
  3. 复制 HttpTransportSE.java ServiceConnectionSE.java (我还需要复制交通运输的.java ServiceConnection.java HeaderProperty.java )。删除这些文件的进口,并确保他们所使用的文件(而不是进口 ksoap2.jar
  4. 使用rallat答案(我复制粘贴吧):

  1. Put ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar in a project
  2. Download ksoap2 sources from https://github.com/mosabua/ksoap2-android/tree/ (ksoap2 repository)
  3. Copy HttpTransportSE.java, ServiceConnectionSE.java (I also needed to copy Transport.java, ServiceConnection.java and HeaderProperty.java). Delete imports from those files and make sure that they use your files (not imports from ksoap2.jar)
  4. Use rallat answer ( I copy-pasted it):

  • ServiceConnectionSE.java 添加此为接受不受信任的证书:

  • ServiceConnectionSE.java add this for accept untrusted certificate:

private TrustManager[] trustAllCerts = new TrustManager[] {
    new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
        public void checkClientTrusted(
            java.security.cert.X509Certificate[] certs, String authType) {
        }
        public void checkServerTrusted(
            java.security.cert.X509Certificate[] certs, String authType) {
        }
    }
};

  • 然后使用此构造函数来 允许不受信任的证书,而不是 经过验证的主机名:

  • then use this constructors to allow untrusted certificates and not verified hostnames:

    public ServiceConnectionSE(String url) throws IOException {
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
            e.getMessage();
        }
        connection = (HttpsURLConnection) new URL(url).openConnection();
        ((HttpsURLConnection) connection).setHostnameVerifier(new AllowAllHostnameVerifier());
    }    
    

  • 第二个构造器

  • Second contructor

    public ServiceConnectionSE(Proxy proxy, String url) throws IOException {
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
         } catch (Exception e) {
            e.getMessage();
         }
         connection = (HttpsURLConnection) new URL(url).openConnection();
        ((HttpsURLConnection) connection).setHostnameVerifier(new AllowAllHostnameVerifier());
    
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);
    }
    

  • 在你的code只需使用:

    In your code just use:

    HttpTransportSE aht = new HttpTransportSE(URL);    
    aht.call(SOAP_ACTION, envelope);
    

    其他的事情在教程

    这篇关于KSOAP 2的Andr​​oid使用HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆