0

I have implemented HTTP Post to post data to the backend. How do I implement HTTPS in Android (I have already configured the backend for https)?

I googled and found some solutions: Secure HTTP Post in Android

and tried them but I do not receive any data in the backend.

Is it the correct way to implement? Is there any other method?

Below is my code snippet:

              File file = new File(filepath);
         HttpClient client = new DefaultHttpClient();

             //String url = "http://test.....;
             String url = "https://test......";

         HttpPost post = new HttpPost(url);
             FileEntity bin = new FileEntity(file, url);

             post.setEntity(bin);

             HttpResponse response =  client.execute(post); 

             HttpEntity resEntity = response.getEntity();

Basically I am using fileentity to do a HTTPPost. Now I want to do this over https. After implementing https over at the backend I just modified http to https in the url and tested again. And it is not working.

Any idea how do i resolve this?

Thanks In Advance, Perumal

Community
  • 1
  • 1
perumal316
  • 1,159
  • 6
  • 17
  • 36
  • If you've implemented a call that uses AndroidHttpClient, HttpURLConnection or something similar, you should just be able to change the URL the application POSTs to. – mopsled Aug 02 '11 at 15:34

1 Answers1

0

Make sure your http client supports the SSL socket:

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

    HttpParams params = new BasicHttpParams();
    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry); 
    HttpClient httpsClient = new DefaultHttpClient(manager, params);

and use this client to execute your POST request:

    HttpPost post = new HttpPost("https://www.mysecuresite.com");
    post.setHeader("Content-Type", "application/x-www-form-urlencoded");
    post.setEntity(new StringEntity("This is the POST body", HTTP.UTF_8));

    HttpResponse response = httpsClient.execute(post);
ptc
  • 546
  • 6
  • 9
  • I have tried this but it is not working. I have checked the logs and it is giving me javax.net.ssl.SSLPeerUnverifiedException: No peer certificate msg. I am using SDK 2.3.3. – perumal316 Aug 08 '11 at 11:54