How to host HTTPS on Android

I looked through the following links, but nothing seems specific. Secure HTTP in Android This no longer works, I tested it, and there are comments from other people who say that it does not work.

I also checked this: DefaultHttpClient, Certificates, Https and sending problem! It seems like this might work, but the blogger just leaves you hanging. Step-by-step instructions will be helpful. I managed to get my certificate because I could not complete its second step.

http://www.makeurownrules.com/secure-rest-web-service-mobile-application-android.html This seems good, but again, I lose the author in the last step: "Let's go back to our original client rest code." He is everywhere too, I don’t know which libraries he uses. He does not explain his code and

RestTemplate restTemplate = new RestTemplate(); 

this is another clipper. Because this class was not provided. So, if someone can explain how to handle an HTTPS request in detail, that would be great. I need to accept the signed certificate itself.

+8
java android
source share
2 answers

Hope this helps. This is the code that I used and worked perfectly.

 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 as follows: -

 HttpClient httpClient = createHttpClient(); 

and use it with HttpPost.

Hooray!!

EDIT

And I did not use RestTemplate in my code. I made a simple request. If you need more help, just let me know. It seems that I recently did something similar to what you are looking for.

+11
source

This is the method I used for the HTTPS message, and here I used the user certificate, so change the destination of the HttpClient with your own ...

 public String postData(String url, String xmlQuery) { final String urlStr = url; final String xmlStr = xmlQuery; final StringBuilder sb = new StringBuilder(); Thread t1 = new Thread(new Runnable() { public void run() { HttpClient httpclient = MySSLSocketFactory.getNewHttpClient(); HttpPost httppost = new HttpPost(urlStr); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>( 1); nameValuePairs.add(new BasicNameValuePair("xml", xmlStr)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); Log.d("Vivek", response.toString()); HttpEntity entity = response.getEntity(); InputStream i = entity.getContent(); Log.d("Vivek", i.toString()); InputStreamReader isr = new InputStreamReader(i); BufferedReader br = new BufferedReader(isr); String s = null; while ((s = br.readLine()) != null) { Log.d("YumZing", s); sb.append(s); } Log.d("Check Now",sb+""); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } /* * catch (ParserConfigurationException e) { // TODO * Auto-generated catch block e.printStackTrace(); } catch * (SAXException e) { // TODO Auto-generated catch block * e.printStackTrace(); } */ } }); t1.start(); try { t1.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Getting from Post Data Method "+sb.toString()); return sb.toString(); } 
0
source

All Articles