title: Connect to HTTPS from Android - REST and SOAP web service
body: <p>I am facing a problem to call https from Android. Can any one please help with steps to connect to the https server.</p>
  
  <p>I tried to connect to google with https and its working fine but when I try to connect to my local server, I am facing problems.</p>
  
  <ol>
  <li>want to connect a RESTful web service with https</li>
  <li><p>want to connect a SOAP based web service developed using JAX-WS with https.</p></li>
  <li><p>Code to connect with RESTFul</p>
  
  <pre><code>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();
   }
  </code></pre>
  
  <p>its working fine for google but not working for my server and it's giving</p>
  
  <p>javax.net.ssl.SSLException: Not trusted server certificate</p></li>
  <li><p>Code for connect with SOAP:</p>
  
  <pre><code>public String getString(final String methodName, Map&lt;String, Object&gt;        params)        throws     Exception {
  HttpTransportSE httpsTransportSE = new         HttpTransportSE("https://ipaddress:8181/BankingWS/banking?wsdl");
  
  try {
      SoapObject request = new SoapObject("https://test/",
              methodName);
      if(params != null &amp;&amp; params.size() &gt; 0){
          Set&lt;String&gt; keys = params.keySet();
          Iterator&lt;String&gt; 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;
  }
  }
  </code></pre>
  
  <p>In above code using the TrustManagerManipulator from following link:
  <a href="http://abhinavasblog.blogspot.com/2011/07/allow-untrusted-certificate-for-https.html" rel="nofollow">http://abhinavasblog.blogspot.com/2011/07/allow-untrusted-certificate-for-https.html</a></p></li>
  </ol>
  
  <p>This is also not working and when I check the response code it's giving</p>
  
  <pre><code>SoapFault - faultcode: 'S:Client' faultstring: 'Cannot find dispatch method for {test.WSEndPointPort}authenticate' faultactor: 'null' detail: null
  </code></pre>
  
  <p>Please help to fix this problem, as I am not able to call https from Android by any way :(</p>
  
  <p>Thank you very much for your time and efforts.</p>
  
  <p>Thank you,
  Ishan</p>