HttpClient Use Instances

Source: Internet
Author: User

How to use

Using HttpClient to send a request and receive a response is simple and typically requires the following steps.

1. Create the 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 the HttpGet, HttpPost common setparams (Hetpparams params) method to add the request parameters, and for HttpPost objects, you can also call 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 HttpResponse getentity () The Httpentity method gets the object that wraps the server's response content. 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


Httputil httpclientutil = new Httputil (); map<string,object> map = new hashmap<string, object> () map.put ("OrderInfo", json.tostring ()); String str = httpclientutil.postmethod (order_info_url, map);



/** *  Access  *  @param  url  request address  *  @param  argsMap  carried parameters with post method  *  @return   String  return results  *  @throws  exception */public static  string postmethod (String url,map<string, object> argsmap)  throws  exception{byte[] databyte = null; Httpclient httpclient = new defaulthttpclient (); Httppost httppost = new httppost (URL);//Set parameter urlencodedformentity encodedformentity  = new urlencodedformentity (Sethttpparams (argsmap),  "UTF-8"); Httppost.setentity ( encodedformentity);//  Execution Request Httpresponse httpresponse = httpclient.execute (httpPost);//   Gets the returned data httpentity httpentity = httpresponse.getentity ();if  (httpentity !=  NULL)  {byte[] responsebytes = getdata (httpentity);d atabyte = responsebytes; Httppost.abort ();} Converts a byte array to a string strIng result = bytestostring (databyte); return result;} /** *  set HttpPost request parameters  *  @param  argsMap *  @return  basichttpparams * /private static list<basicnamevaluepair> sethttpparams (Map<String, Object> &NBSP;ARGSMAP) {list<basicnamevaluepair> namevaluepairlist = new arraylist< Basicnamevaluepair> ();//Set request parameter if  (Argsmap!=null && !argsmap.isempty ())  {Set< Entry<string, object>> set = argsmap.entryset (); iterator<entry<string,  object>> iterator = set.iterator (); while (Iterator.hasnext ()) {Entry<String,  Object> entry = iterator.next (); Basicnamevaluepair basicnamevaluepair = new basicnamevaluepair (Entry.getKey (),  Entry.getvalue (). toString ()); Namevaluepairlist.add (Basicnamevaluepair);}} Return namevaluepairlist;}


This article from "Ertan to win" blog, declined reprint!

HttpClient Use Instances

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.