HttpClient POST Request example (no parameter name and example with parameter name)

Source: Internet
Author: User

Version: 4.1

    • Case with parameter name
HttpClient HttpClient = new Defaulthttpclient ();
HttpPost HttpPost =Newhttppost (URL);//Httppost.setheader ("accept-encoding", "gzip,deflate");//indicates that the returned data is compressed in ZIP formatString Postparam = "";//the parameter contents of the request List<NameValuePair> Nvps =NewArraylist<namevaluepair>(); Nvps.add (NewBasicnamevaluepair ("Data", Postparam)); Httppost.setentity (NewUrlencodedformentity (Nvps, "Utf-8")); HttpResponse Response=Httpclient.execute (HttpPost); Httpentity Entity=response.getentity (); if(Response.getstatusline (). Getstatuscode () = = 200) {if(Entity.getcontentencoding (). toString (). Equalsignorecase ("Content-encoding:gzip") {response.setentity (NewGzipdecompressingentity (Response.getentity ()));//Unzip the zipentity =response.getentity (); } String responsecontent=entityutils.tostring (entity); System.out.println ("Responsecontent: \ n" +responsecontent); }}
    • Case with no parameter name

HttpClient HttpClient =Newdefaulthttpclient (); HttpPost HttpPost=Newhttppost (URL);//Httppost.setheader ("accept-encoding", "gzip,deflate");//indicates that the returned data is compressed in ZIP format String postparam = "";//requested parameter contents  stringentity paramentity = new     stringentity (Postparam);//no parameter name, only parameter content httppost.setentity (paramentity); HttpResponse Response=Httpclient.execute (HttpPost); Httpentity Entity=response.getentity (); if(Response.getstatusline (). Getstatuscode () = = 200) {if(Entity.getcontentencoding (). toString (). Equalsignorecase ("Content-encoding:gzip") {response.setentity (NewGzipdecompressingentity (Response.getentity ()));//Unzip the zipentity =response.getentity (); } String responsecontent=entityutils.tostring (entity); System.out.println ("Responsecontent: \ n" +responsecontent); }}


    • Class structure diagram of httpentity

HttpClient POST Request example (no parameter name and example with parameter name)

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.