Maple Snow Court Source: http://www.cnblogs.com/FengXueTing-px/Welcome Reprint
HttpClient get and POST requests for Java learning experience
1. Preface
2. Get request
3. Post request
First, preface
This blog post records the Get and post requests for httpclient
This article is based on the following articles:
http://huangqiqing123.iteye.com/blog/2054436 (httpclient AddHeader and SetHeader)
http://zywang.iteye.com/blog/916834 (send get and POST requests using Apache HttpClient access JSP)
Http://www.linuxidc.com/Linux/2012-02/55502p3.htm (Detailed use of HttpClient 4.0)
Second, GET request
Examples of GET requests are as follows:
//httpClientHttpClient HttpClient =Newdefaulthttpclient (); //Get MethodHttpGet HttpGet =NewHttpGet ("https://api.microsofthealth.net/v1/me/Summaries/Daily"); //Set HeaderString au= "Bearer" +Access_token; Httpget.setheader ("Authorization", Au); //ResponseHttpResponse response =NULL; Try{Response=Httpclient.execute (HttpGet); }Catch(Exception e) {}//get response into StringString temp= ""; Try{httpentity entity=response.getentity (); Temp=entityutils.tostring (Entity, "UTF-8"); }Catch(Exception e) {}returnTemp
Third, POST request:
Examples of GET requests are as follows:
//httpClientHttpClient HttpClient =Newdefaulthttpclient (); //Get MethodHttpPost HttpPost =NewHttpPost ("HTTPS://LOGIN.LIVE.COM/OAUTH20_TOKEN.SRF"); //Set HeaderHttppost.setheader ("Content-type", "application/x-www-form-urlencoded"); //Set paramslist<namevaluepair> params =NewArraylist<namevaluepair>(); Params.add (NewBasicnamevaluepair ("client_id", client_id)); Params.add (NewBasicnamevaluepair ("Redirect_uri", Redirect_uri)); Params.add (NewBasicnamevaluepair ("Client_secret", Client_secret)); Params.add (NewBasicnamevaluepair ("Code",code)); Params.add (NewBasicnamevaluepair ("Grant_type", "Authorization_code")); Try{httppost.setentity (Newurlencodedformentity (params)); }Catch(Exception e) {}//ResponseHttpResponse response =NULL; Try{Response=Httpclient.execute (HttpPost); }Catch(Exception e) {}//get response into StringString temp= ""; Try{httpentity entity=response.getentity (); Temp=entityutils.tostring (Entity, "UTF-8"); }Catch(Exception e) {}returnTemp
HttpClient get and POST requests for Java learning experience