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