PC requests and carries cookies through HTTP

Source: Internet
Author: User
Tags error status code
Package COM. landray. kmss. app. oa. sign. util; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import Org. apache. HTTP. httpentity; import Org. apache. HTTP. httpresponse; import Org. apache. HTTP. namevaluepair; import Org. apache. HTTP. client. cookiestore; import Org. apache. HTTP. client. entity. urlencodedformentity; import Org. apache. HTTP. client. methods. httpget; import Org. Apache. HTTP. cookie. cookie; import Org. apache. HTTP. impl. client. defaulthttpclient; import Org. apache. HTTP. message. basicnamevaluepair; import Org. apache. HTTP. params. httpconnectionparams; import Org. apache. HTTP. params. httpparams; import Org. apache. HTTP. util. entityutils; public class httputils {protected final static string http_charset = "UTF-8"; public static cookiestore dologin (string urlpath, string Logi Nname, string loginpass) {defaulthttpclient httpclient = new defaulthttpclient (); httpparams = httpclient. getparams (); httpconnectionparams. setconnectiontimeout (httpparams, 5000); httpconnectionparams. setsotimeout (httpparams, 10000); try {list <namevaluepair> pairs = new arraylist <namevaluepair> (); pairs. add (New basicnamevaluepair ("j_username", loginname); pairs. add (New basicnamevalue Pair ("j_password", loginpass); urlpath + = "? "+ Entityutils. tostring (New urlencodedformentity (pairs, http_charset); system. out. println (urlpath); httpget = new httpget (urlpath); httpresponse = httpclient.exe cute (httpget); int statuscode = httpresponse. getstatusline (). getstatuscode (); If (statuscode! = 200) {httpget. abort (); throw new runtimeexception ("httpclient, error Status Code:" + statuscode);} return httpclient. getcookiestore ();} catch (exception e) {e. printstacktrace ();} finally {httpclient. getconnectionmanager (). shutdown ();} return NULL;} public static string httpget (string urlpath, hashmap <string, string> Params, cookiestore logincookie) {defaulthttpclient httpclient = new defau Lthttpclient (); httpclient. setcookiestore (logincookie); httpparams = httpclient. getparams (); httpconnectionparams. setconnectiontimeout (httpparams, 5000); httpconnectionparams. setsotimeout (HTTP Params, 10000); try {If (Params! = NULL &&! Params. isempty () {list <namevaluepair> pairs = initurlparams (Params); urlpath + = "? "+ Entityutils. tostring (New urlencodedformentity (pairs, http_charset);} httpget = new httpget (urlpath); httpresponse = httpclient.exe cute (httpget); int statuscode = httpresponse. getstatusline (). getstatuscode (); If (statuscode! = 200) {httpget. abort (); throw new runtimeexception ("httpclient, error Status Code:" + statuscode);} httpentity entity = httpresponse. getentity (); string result = NULL; If (entity! = NULL) {result = entityutils. tostring (entity, "UTF-8");} // entityutils. consume (entity); return result;} catch (exception e) {e. printstacktrace ();} finally {httpclient. getconnectionmanager (). shutdown ();} return NULL;} public static string httppost (string urlpath, hashmap <string, string> Params, cookiestore logincookie) {defaulthttpclient httpclient = new defaulthttpclient (); httpclient. SETCO Okiestore (logincookie); httpparams = httpclient. getparams (); httpconnectionparams. setconnectiontimeout (httpparams, 5000); response. setsotimeout (httpparams, 10000); try {If (Params! = NULL &&! Params. isempty () {list <namevaluepair> pairs = initurlparams (Params); urlpath + = "? "+ Entityutils. tostring (New urlencodedformentity (pairs, http_charset);} httpget = new httpget (urlpath); httpresponse = httpclient.exe cute (httpget); int statuscode = httpresponse. getstatusline (). getstatuscode (); If (statuscode! = 200) {httpget. abort (); throw new runtimeexception ("httpclient, error Status Code:" + statuscode);} httpentity entity = httpresponse. getentity (); string result = NULL; If (entity! = NULL) {result = entityutils. tostring (entity, "UTF-8");} // entityutils. consume (entity); return result;} catch (exception e) {e. printstacktrace ();} finally {httpclient. getconnectionmanager (). shutdown ();} return NULL;} Private Static list <namevaluepair> initurlparams (hashmap <string, string> Params) {list <namevaluepair> pairs = new arraylist <namevaluepair> (Params. size (); For (map. entry <stri Ng, string> entry: Params. entryset () {string value = entry. getvalue (); If (value! = NULL) {pairs. add (New basicnamevaluepair (entry. getkey (), value) ;}} return pairs;} // public static string get (string urlpath, string cookieid) throws ioexception {// URL url = new URL (urlpath); // send an HTTP request // httpurlconnection conn = (httpurlconnection) URL. openconnection (); // pass in the Single Sign-On token // Conn. setrequestproperty ("cookie", cookieid); // Conn. connect (); // inputstream is = Conn. getinputstream (); // Bufferedreader reader = new bufferedreader (New inputstreamreader (// is, "UTF-8"); // stringbuffer Buf = new stringbuffer (); // string line = NULL; // while (line = reader. readline ())! = NULL) {// Buf. append (line); //} // is. close (); // release the connection // Conn. disconnect (); // get XML // return Buf. tostring (); //} public static void main (string [] ARGs) {string urlpath = "http: // 192.168.0.158: 8080/ekp /"; string loginname = "admin"; string loginpass = "1"; cookiestore cookie = dologin (urlpath + "j_acegi_security_check", loginname, loginpass); List <cookie> cookies = cookies. getcookies (); string Cookieid = NULL; For (cookie C: cookies) {system. out. println (C. getname () + ":" + C. getvalue (); cookieid = C. getvalue ();} urlpath + = "km/review/km_review_index/kmreviewindex. do? Method = List & nodetype = template & Q. fdtemplate = 147ce24167648cf71c389ff4e7295d0e & orderby = doccreatetime & ordertype = down & __seq = "; system. out. println (urlpath); string httpget = httpget (urlpath, null, cookie); system. out. println (httpget. trim ());}}

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.