1

In my app, I need to create a registration form with username and password combination.But it needs to go to the server encrypted.Could you please let me know if I can use SSL for that and if yes, how to use it?

Thank you.

Ingrid Cooper
  • 1,161
  • 2
  • 15
  • 26

1 Answers1

1

I'm making a guess, but if you want an actual handshake to occur, you have to let android know of your certificate. If you want to just accept no matter what, then use this pseudo-code to get what you need with the Apache HTTP Client:

        SchemeRegistry schemeRegistry = new SchemeRegistry ();

        schemeRegistry.register (new Scheme ("http",
        PlainSocketFactory.getSocketFactory (), 80));
        schemeRegistry.register (new Scheme ("https",
        new CustomSSLSocketFactory (), 443));

        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager (
           params, schemeRegistry);


         return new DefaultHttpClient (cm, params);
    }

    // CustomSSLSocketFactory:

       public class CustomSSLSocketFactory extends org.apache.http.conn.ssl.SSLSocketFactory
         {
           private SSLSocketFactory FACTORY =  HttpsURLConnection.getDefaultSSLSocketFactory ();

        public CustomSSLSocketFactory ()
          {
            super(null);  
        try
           {
            SSLContext context = SSLContext.getInstance ("TLS");
            TrustManager[] tm = new TrustManager[] { new FullX509TrustManager () };
             context.init (null, tm, new SecureRandom ());

             FACTORY = context.getSocketFactory ();
          }
         catch (Exception e)
         {
              e.printStackTrace();
         }
    }

   public Socket createSocket() throws IOException
     {
        return FACTORY.createSocket();
      }

     // TODO: add other methods like createSocket() and getDefaultCipherSuites().
   // Hint: they all just make a call to member FACTORY 
}


   //FullX509TrustManager is a class that implements javax.net.ssl.X509TrustManager,    yet   none of the methods actually perform any work, get a sample here.
Pawan Yadav
  • 1,827
  • 19
  • 17