HttpClient POST request data

Source: Internet
Author: User

Recently, you need to add POST request data in your project, and the previous get request was to use the urlconnection that came with the JDK. Start using HttpClient with the recommendation of the project team staff.

HttpClient Introduction:

HttpClient is a sub-project under Apache Jakarta common to provide an efficient, up-to-date, feature-rich client programming toolkit that supports the HTTP protocol.

And it supports the latest version and recommendations of the HTTP protocol. HttpClient has been used in many projects, such as the two other open source projects that are famous on Apache Jakarta Cactus

And Htmlunit both used httpclient.

HttpClient the steps to send the request:

(1) Create a HttpClient object.

(2) Create an instance of the request method and specify the request URL. Create a HttpGet object if you need to send a GET request, or create a HttpPost object if you need to send a POST request.

(3) If you need to send request parameters, you can call HttpGet, HttpPost common setparams (Hetpparams params) method to add the request parameters, for HttpPost object,

You can also call the setentity (httpentity entity) method to set the request parameters.

(4) Call the Execute (httpurirequest request) of the HttpClient object to send the request, which returns a HttpResponse.

(5) Call HttpResponse's Getallheaders (), Getheaders (String name) and other methods to get the server's response header; call the GetEntity () method of HttpResponse

Gets the Httpentity object that wraps the response content of the server. This object is used by the program to obtain the server's response content.

(6) Release the connection. The connection must be released regardless of the success of the execution method

So, next follow this step, write the POST request code as follows:

public string doPost (string url, map<string,string> Map, string charset) {
Closeablehttpclient httpClient = null;
HttpPost httppost = null;
String result = null;
try{
HttpClient = Httpclients.createdefault ();
HttpPost = new HttpPost (URL);
Setting parameters
list<namevaluepair> list = new arraylist<namevaluepair> ();
Iterator Iterator = Map.entryset (). Iterator ();
while (Iterator.hasnext ()) {
map.entry<string,string> Elem = (map.entry<string, string>) Iterator.next ();
List.add (New Basicnamevaluepair (Elem.getkey (), Elem.getvalue ()));
}
if (list.size () > 0) {
urlencodedformentity entity = new urlencodedformentity (list,charset);
Httppost.setentity (entity);
}
HttpResponse response = Httpclient.execute (HttpPost);
if (response! = NULL) {
Httpentity resentity = response.getentity ();
if (resentity! = null) {
result = Entityutils.tostring (Resentity,charset);
}
}
}catch (Exception ex) {
Ex.printstacktrace ();
}
return result;
}

The test code is as follows:

    public static void Main (string[] args) throws Unsupportedencodingexception {
String url = "Http://admin.tingwen.me/index.php/api/interfaceXykj/touList";
map<string,string> params = new hashmap<> ();
Params.put ("page", "100");
HttpClient client = new HttpClient ();
String result = Client.dopost (Url,params, "UTF-8");
System.out.println (unicodetostring (result));
}

Test results:

{
"status": 1,
" msg": "Query Headline News success!",
"Results": [
           {
"id": "65678",
" tuiid": "0",
" post_author": "8909",
" post_news": "161",
" Author": "0",
" post_keywords": "Snap, drone, technology",
" post_date": "2017-03-03 16:19:43",
" post_times": "",
" post_content": "",
" post_title": "Transfer snap company is developing unmanned aerial vehicles: Implement the Company philosophy",
" Post_lai": "Sina Technology",
" post_mp": "Http://mp3.tingwen.me/data/upload/mp3/58b9270c35694.mp3",
" post_time": "105000",
" post_size": "1268345 "
           }
       ]
}

HttpClient POST request data

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.