1

In android RESTFUL webservice,

  1. In HttpClient client = new DefaultHttpClient(); - I get successful response.

  2. But Now my URL changed to http to https(SSL). - And this url not trusted certificate.

so I use following @Daniel code.

Trusting all certificates using HttpClient over HTTPS

  1. If I Use client = WebClientDevWrapper.getNewHttpClient(); instead of DefaultClient() - I get sessionExpiredException error.

any help??

Community
  • 1
  • 1
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159

1 Answers1

3

I hope you are using this method

public void connect() throws Exception  {

        HttpPost post = new HttpPost(new URI(""));
        post.setEntity(new StringEntity(""));
        trustAll();
        HttpClient client = new DefaultHttpClient();
        HttpResponse result = client.execute(post);
    }

Add this code before the HttpsURLConnection and it will be done.

private void trustAll() { 
    try { 
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){ 
                    public boolean verify(String hostname, SSLSession session) { 
                            return true; 
                    }}); 
            SSLContext context = SSLContext.getInstance("TLS"); 
            context.init(null, new X509TrustManager[]{new X509TrustManager(){ 
                    public void checkClientTrusted(X509Certificate[] chain, 
                                    String authType) throws CertificateException {} 
                    public void checkServerTrusted(X509Certificate[] chain, 
                                    String authType) throws CertificateException {} 
                    public X509Certificate[] getAcceptedIssuers() { 
                            return new X509Certificate[0]; 
                    }}}, new SecureRandom()); 
            HttpsURLConnection.setDefaultSSLSocketFactory( 
                            context.getSocketFactory()); 
    } catch (Exception e) { 
            Log.e("TAG",e); 
    } 
}

this method worked for me to accept the certificates and also try increasing the session time out.

Piyush
  • 1,973
  • 1
  • 19
  • 30