Hello;

           I got the easy solution for call https web service from our
application code. Here i am describing code for call https protocol
web service.

           Now there will be no error regarding SSL(server certificate
not valid).
           Use following class for opening connection with HTTPS web
service and before connect(con.connect()) to that web service.

package com.demo.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.ksoap2.transport.ServiceConnection;
import com.demo.ioutil.IOUtils;
import android.util.Log;


public class HttpsDemo implements ServiceConnection {

            private HttpURLConnection connection;
            private HttpsURLConnection con;

            public HttpsDemo (String url) throws IOException {


                //With HTTPS

                 try {
                 SSLContext sc = SSLContext.getInstance("TLS");
                 sc.init(null, new TrustManager[] { new
MyTrustManager() }, new SecureRandom());

 
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                 HttpsURLConnection.setDefaultHostnameVerifier(new
MyHostnameVerifier());
                 con = (HttpsURLConnection) new
URL(url).openConnection();
                 con.setConnectTimeout(10000);
                 con.setUseCaches(false);
                 con.setDoOutput(true);
                 con.setDoInput(true);

                /*For  Simple HTTP
                connection = (HttpURLConnection) new
URL(url).openConnection();
                connection.setUseCaches(false);
                connection.setDoOutput(true);
                connection.setDoInput(true);

                 connection.connect();

                 BufferedReader br = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
                 StringBuffer sb = new StringBuffer();
                 String line;
                 while ((line = br.readLine()) != null)
                         sb.append(line);


                 Log.e("HTTP", sb.toString() );
                 Log.e("HTTP", "END" );
                 */


         } catch (Exception e) {
                Log.e("HTTPS", e.getMessage() );
         }


     }

            public void connect() throws IOException {
                con.connect();
            }

            public void disconnect() {
                con.disconnect();
            }

            public void setRequestProperty(String string, String soapAction)
{
                con.setRequestProperty(string, soapAction);
            }

            public void setRequestMethod(String requestMethod) throws
IOException {
                con.setRequestMethod(requestMethod);
            }

            public OutputStream openOutputStream() throws IOException {
                return con.getOutputStream();
            }

            public InputStream openInputStream() throws IOException {
                return con.getInputStream();
            }

            public InputStream getErrorStream() {
                return con.getErrorStream();
            }




            /**
             * MyHostnameVerifier
             */
            private class MyHostnameVerifier implements HostnameVerifier {

                    @Override
                    public boolean verify(String hostname, SSLSession
session) {
                            return true;
                    }


            }

            /**
             * MyTrustManager
             */
            private class MyTrustManager implements X509TrustManager {

                    @Override
                    public void checkClientTrusted(X509Certificate[] chain,
String authType) {
                    }

                    @Override
                    public void checkServerTrusted(X509Certificate[] chain,
String authType) {
                    }

                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                            return null;
                    }
            }

}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to