Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to HTTPS post in Android

I have looked at the following links, but nothing seems concrete. Secure HTTP Post in Android This one does not work anymore, I have tested it and there are comments from other people saying it does not work.

I also checked this out: DefaultHttpClient, Certificates, Https and posting problem! This seems it could work but the blogger just leaves you hanging. More step by step instructions would be helpful. I managed to get my certificate by I have not been able to follow through his second step.

http://www.makeurownrules.com/secure-rest-web-service-mobile-application-android.html This one seem good, but again, I loose the author at the last step: "Back to our original rest client code." He too is all over the place, I have no clue which libraries he is using. He is not explaining his code and with the

RestTemplate restTemplate = new RestTemplate();

it's another cliffhanger. Because that class has not been provided. So, if someone could explain how to do HTTPS post request in detail that would be great. I do need to accept the self signed certificate.

like image 487
Noman Arain Avatar asked Jul 16 '12 12:07

Noman Arain


1 Answers

I hope it would help. This is the code i used and worked perfectly fine.

private HttpClient createHttpClient()
{
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);

    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);

    return new DefaultHttpClient(conMgr, params);
}

Then create an HttpClient like this: -

HttpClient httpClient = createHttpClient();

and use it with HttpPost.

Cheers!!

EDIT

And i did not used RestTemplate in my code. I made a simple post request. If you need more help just let me know. It seems like i recently have done something similar to what you are looking for.

like image 79
Varundroid Avatar answered Oct 19 '22 17:10

Varundroid