Httpclient4.3 simple Encapsulation

Source: Internet
Author: User

A simple encapsulation of httpclient4.3. The following code

/*** Httputil tool class ** @ author Rex */public class httputil {Private Static closeablehttpclient client; Private Static basiccookiestore cookiestore; Private Static httpget get; Private Static httppost; private Static httpresponse response; Private Static result; Private Static httpentity entity; /*** // todo GET request ** @ Param URL request address * @ Param headers Request Header * @ Param Params request parameter * @ Param ENC ODing Request Encoding * @ return * @ throws ioexception * @ throws clientprotocolexception */public static result get (string URL, Map <string, string> headers, Map <string, string> Params) throws clientprotocolexception, ioexception {cookiestore = new basiccookiestore (); client = httpclients. custom (). setdefacookcookiestore (cookiestore ). build (); url = (null = Params? URL: URL + parseparam (Params); get = new httpget (URL); get. setheaders (parseheader (headers); response = client.exe cute (get); entity = response. getentity (); Result = new result (); result. sethttpclient (client); result. setcookies (cookiestore. getcookies (); result. setstatuscode (response. getstatusline (). getstatuscode (); result. setheaders (response. getallheaders (); result. sethttpentity (entity); resul T. setbody (entityutils. tostring (entity); return result ;} /*** // todo POST request ** @ Param URL request address * @ Param headers Request Header * @ Param Params request parameter * @ Param encoding Request Encoding * @ return * @ throws ioexception * @ throws clientprotocolexception */public static result post (string URL, map <string, string> headers, Map <string, string> Params, string encoding) throws clientprotocolexception, ioexception {cookiestor E = new basiccookiestore (); client = httpclients. custom (). setdefacookcookiestore (cookiestore ). build (); Post = new httppost (URL); List <namevaluepair> List = new arraylist <namevaluepair> (); For (string temp: Params. keyset () {list. add (New basicnamevaluepair (temp, Params. get (temp);} post. setentity (New urlencodedformentity (list, encoding); Post. setheaders (parseheader (headers); response = client. Execute (post); entity = response. getentity (); Result = new result (); result. sethttpclient (client); result. setcookies (cookiestore. getcookies (); result. setstatuscode (response. getstatusline (). getstatuscode (); result. setheaders (response. getallheaders (); result. sethttpentity (entity); result. setbody (entityutils. tostring (entity); return result;}/*** // todo conversion header ** @ Param headers * @ return */ Private Static header [] parseheader (Map <string, string> headers) {If (null = headers | headers. isempty () {return getdefaultheaders ();} header [] allheader = new basicheader [headers. size ()]; int I = 0; For (string STR: headers. keyset () {allheader [I] = new basicheader (STR, headers. get (STR); I ++;} return allheader;}/*** // todo default header ** @ return */Private Static header [] getdefaulthea Ders () {header [] allheader = new basicheader [2]; allheader [0] = new basicheader ("Content-Type ", "Application/X-WWW-form-urlencoded"); allheader [1] = new basicheader ("User-Agent", "Mozilla/5.0 (Windows NT 6.3; wow64) applewebkit/537.36 (khtml, like gecko) Chrome/33.0.1750.146 Safari/537.36 "); Return allheader ;} /*** // todo conversion parameter list ** @ Param Params * @ return */Private Static string parseparam (MA P <string, string> Params) {If (null = Params | Params. isempty () {return "";} stringbuffer sb = new stringbuffer (); For (string key: Params. keyset () {sb. append (Key + "=" + Params. get (key) + "&");} return sb. substring (0, sb. length ()-1);}/*** release the httpclient object */public static void closeclient (closeablehttpclient client) {If (null! = Client) {try {client. Close ();} catch (ioexception e) {e. printstacktrace ();}}}}

/*** Encapsulate the returned result of the Request * @ author Rex **/public class result {private closeablehttpclient httpclient; private list <cookie> cookies; private httpentity; private hashmap <string, header> headerall; private int statuscode; private string body; public list <cookie> getcookies () {return cookies;} public void setcookies (list <cookie> cookies) {This. cookies = cookies;} public closeablehttpclient gethttpclient () {return httpclient;} public void sethttpclient (closeablehttpclient httpclient) {This. httpclient = httpclient;} public int getstatuscode () {return statuscode;} public void setstatuscode (INT statuscode) {This. statuscode = statuscode;} public void setheaders (header [] headers) {headerall = new hashmap <string, header> (); For (header: headers) {headerall. put (header. getname (), header) ;}} public hashmap <string, header> getheaderall () {return headerall;} public void sethttpentity (httpentity entity) {This. httpentity = entity;} public httpentity gethttpentity () {return httpentity;} Public String getbody () {return body;} public void setbody (string body) {This. body = body ;}}

One test of logging on to Xiaomi

Public class xiaomilogin {public static void main (string [] ARGs) {string login_url = "https://account.xiaomi.com/pass/serviceLoginAuth2"; Map <string, string> Params = new hashmap <string, string> (); params. put ("user", "Xiaomi account"); Params. put ("PWD", "password"); Params. put ("Callback", "https://account.xiaomi.com"); Params. put ("Sid", "Passport"); Params. put ("display", "mobile"); Params. put ("QS", "% 3 fsid % 3 dpassport"); Params. put ("_ sign", "kkrvcpzodc + gldeyosdmhwv0xg ="); try {result R = httputil. post (login_url, null, Params, "UTF-8"); For (Cookie: R. getcookies () {system. out. println (cookie. getname () + "=" + Cookie. getvalue ();} httputil. closeclient (R. gethttpclient ();} catch (clientprotocolexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();}}}




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.