Use the httpclient framework in Android to send post requests

Source: Internet
Author: User

/*** The httpclientPost Request Method ** @ param username * @ param password * @ return null indicates that the obtained path is incorrect, text returns the requested data */public static String httpclientPost (String username, String password) {try {// 1. open a browser: HttpClient client = new DefaultHttpClient (); // 2. input address String path = "http: // 172.22.64.156: 8080/0001 AndroidWebService/LoginServlet"; HttpPost post = new HttpPost (); // specify the List of submitted data entities
 
  
Parameters = new ArrayList
  
   
(); Parameters. add (new BasicNameValuePair ("username", username); parameters. add (new BasicNameValuePair ("password", password); post. setEntity (new UrlEncodedFormEntity (parameters, "UTF-8"); // 3, press ENTER HttpResponse response = client.exe cute (post); // get The Returned code int code = response. getStatusLine (). getStatusCode (); if (code = 200) {// obtain the returned information InputStream is = response. getEntity (). getContent (); String text = StreamUtil. readStream (is); return text;} else {return null ;}} catch (Exception e) {e. printStackTrace (); return null ;}}
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.