HttpClient Way to submit data to the server

Source: Internet
Author: User

Get mode:
//Use HttpClient request server to send user password to server authentication
try{
String Path = "Http://192.168.13.83:8080/xuexi/servlet/LoginServlet?username=" +urlencoder.encode (Usern Ame, "utf-8") + "&pwd=" +urlencoder.encode (password, "Utf-8");
//1. Creating a HttpClient Object
HttpClient HttpClient = new Defaulthttpclient ();
                
//2. How to set the request
httpget httpget = new HttpGet (path);
//3. Executing an HTTP request
HttpResponse response = Httpclient.execute (httpget);
//4. Get the status code of the request,
statusline statusline = Response.getstatusline ();
int code = Statusline.getstatuscode ();
                
//5. Get content after judging status code
if (code = =) {
httpentity entity = response.getentity ();//Gets the physical contents of the stream information in the encapsulated HTTP request returned
InputStream InputStream = Entity.getcontent ();
//Convert stream information to string
String result = streamutils.streamtostring (InputStream);
                    
Message msg = Message.obtain ();
msg.what = 1;
msg.obj = result;
handler.sendmessage (msg);
                }
                
}catch (Exception e) {
e.printstacktrace ();
                }


Post mode:

//Use Urlconncetion request server to send user password to server authentication
try{
String Path = "Http://192.168.13.83:8080/xuexi/servlet/LoginServlet";
//1. Creating a HttpClient Object
HttpClient HttpClient = new Defaulthttpclient ();
//2. Creating a Request Method
HttpPost httppost = new HttpPost (path);
//Create collection encapsulation data
arraylist<basicnamevaluepair> ArrayList = new arraylist<basicnamevaluepair> ();
Basicnamevaluepair Namevaluepair = new Basicnamevaluepair ("username", username);
Arraylist.add (namevaluepair);
Basicnamevaluepair nameValuePair1 = new Basicnamevaluepair ("pwd", password);
Arraylist.add (NAMEVALUEPAIR1);
                        
//Create an entity
urlencodedformentity entity = new Urlencodedformentity (arrayList, "Utf-8");
//Set content on request
httppost.setentity (entity);
                        
//3. Executes a request that returns a response object
HttpResponse response = Httpclient.execute (httppost);
//4. Getting Status Codes
int code = response.getstatusline (). Getstatuscode ();
//5. Judging and acquiring content
if (code = =) {
httpentity entity1 = response.getentity ();//Gets the entity contents, in which the stream information returned by the HTTP request is included in the encapsulated
InputStream InputStream = Entity1.getcontent ();
//Convert stream information to string
String result = streamutils.streamtostring (InputStream);
Message msg = Message.obtain ();
msg.what = 2;
msg.obj = result;
handler.sendmessage (msg);
                        }

}catch (Exception e) {
e.printstacktrace ();
                }

HttpClient Way to submit data to the server

Related Article

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.