从Android连接到HTTPS - REST和SOAP网络服务

3

我遇到了一个问题,无法从Android设备调用https。请问有谁能提供连接到https服务器的步骤帮助我吗?

我尝试使用https连接到Google并且可以正常工作,但是当我尝试连接到本地服务器时,出现了问题。

  1. want to connect a RESTful web service with https
  2. want to connect a SOAP based web service developed using JAX-WS with https.

  3. Code to connect with RESTFul

    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    
    DefaultHttpClient client = new DefaultHttpClient();
    
    SchemeRegistry registry = new SchemeRegistry();
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
    registry.register(new Scheme("https", socketFactory, 443));
    SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
    DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
    
    // Set verifier     
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
    
    // Example send http request
     //final String url = "https://encrypted.google.com/";
     final String url = "https://ipaddress:8181/RESTTest/cars";
     HttpPost httpPost = new HttpPost(url);
      try{
         HttpResponse response = httpClient.execute(httpPost);
         System.out.println(response);
     }catch(Exception e){
         e.printStackTrace();
     }
    

    its working fine for google but not working for my server and it's giving

    javax.net.ssl.SSLException: Not trusted server certificate

  4. Code for connect with SOAP:

    public String getString(final String methodName, Map<String, Object>        params)        throws     Exception {
    HttpTransportSE httpsTransportSE = new         HttpTransportSE("https://ipaddress:8181/BankingWS/banking?wsdl");
    
    try {
        SoapObject request = new SoapObject("https://test/",
                methodName);
        if(params != null && params.size() > 0){
            Set<String> keys = params.keySet();
            Iterator<String> iterator = keys.iterator();
            while(iterator.hasNext()){
                String key = iterator.next();
                request.addProperty(key, params.get(key));
                key = null;
            }
        }
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
    
        TrustManagerManipulator.allowAllSSL();
        httpsTransportSE.call(methodName, envelope);
    
        SoapPrimitive sp = (SoapPrimitive) envelope.getResponse();
    
        return sp.toString();
    } catch (Exception exception) {
        System.out.println(exception.toString());
        throw exception;
    }
    }
    

    In above code using the TrustManagerManipulator from following link: http://abhinavasblog.blogspot.com/2011/07/allow-untrusted-certificate-for-https.html

这也不起作用,当我检查响应代码时,它会返回错误代码。
SoapFault - faultcode: 'S:Client' faultstring: 'Cannot find dispatch method for {test.WSEndPointPort}authenticate' faultactor: 'null' detail: null

请帮忙解决这个问题,因为我无论如何都无法从安卓设备上调用https :(

非常感谢您的时间和努力。

谢谢, Ishan

1个回答

1

嗨NeTeInStEiN,谢谢您的回复。让我在我的代码中检查一下。谢谢。 - Ishan
2
@NeTeInStEiN 如果我们忽略它,会破坏任何安全性吗?使用SSL的主要目的是保护您在互联网上的通信。如果忽略它,会有什么不同吗? - Scorpion

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接