/** * */ Packageme;ImportOrg.apache.commons.httpclient.Header;Importorg.apache.commons.httpclient.HttpClient;ImportOrg.apache.commons.httpclient.HttpStatus;ImportOrg.apache.commons.httpclient.MultiThreadedHttpConnectionManager;ImportOrg.apache.commons.httpclient.NameValuePair;ImportOrg.apache.commons.httpclient.methods.GetMethod;ImportOrg.apache.commons.httpclient.methods.PostMethod;Importorg.apache.commons.lang3.StringUtils;/*** Created by Lzd on June 27, 2016 morning 9:47:36*/ Public classhttpclient_3x { Public Static voidMain (string[] args)throwsexception{//req ();//Isredirect ();Loginreq (); } //post or GET request Public Static voidReq ()throwsexception{HttpClient HttpClient=NewHttpClient (); //Configure Agents//httpclient.gethostconfiguration (). SetProxy ("127.0.0.1", 80); //Postmethod Postmethod = new Postmethod ("http://java.sun.com");GetMethod GetMethod=NewGetMethod ("http://java.sun.com"); intExecutestatus =Httpclient.executemethod (GetMethod); System.out.println (executestatus);//successful return 200, should be the status code of the response//Print the returned informationString responsebodyasstring =getmethod.getresponsebodyasstring (); System.out.println (responsebodyasstring);//Release Connectiongetmethod.releaseconnection (); } //Get or post append params Private StaticGetMethod Getgetmethod () {return NewGetMethod ("Www.aa.com/a?key = 123"); } Private StaticPostmethod Getpostmethod () {Postmethod Postmethod=NewPostmethod ("www.aa.com/a"); Namevaluepair Namevaluepair=NewNamevaluepair ("Key", "Val"); Postmethod.setrequestbody (NewNamevaluepair[]{namevaluepair}); returnPostmethod; } //detect if redirection is being redirected//301 sc_moved_permanently Page has been permanently moved to another new address//302 sc_moved_temporarily Page temporarily moved to another new address//the address requested by the 303 Sc_see_other client must be accessed through a different URL//307 Sc_temporary_redirect with sc_moved_temporarily Public Static voidIsredirect ()throwsexception{HttpClient HttpClient=NewHttpClient (); GetMethod GetMethod=NewGetMethod ("http://192.168.10.150/pdc/app/book/info/index.html"); Httpclient.executemethod (GetMethod); Getmethod.releaseconnection (); intStatusCode =Getmethod.getstatuscode (); if(StatusCode = = httpstatus.sc_moved_permanently | | statusCode = =httpstatus.sc_moved_temporarily|| StatusCode = =Httpstatus.sc_see_other|| StatusCode = =httpstatus.sc_temporary_redirect) {Header Responseheader= Getmethod.getresponseheader ("Location"); if(Responseheader! =NULL) {String value=Responseheader.getvalue (); if(Stringutils.isnotempty (value)) {GetMethod method=NewGetMethod (value); Httpclient.executemethod (method); //actions after the execution } } } } /** Analog Login * First access to normal page * Get back the results, determine whether to let login, if let login, login and then into. * * */ Public Static voidLoginreq ()throwsexception{String TargetUrl= "/pdc/app/book/info/index.html"; HttpClient HttpClient=NewHttpClient (); Httpclient.gethostconfiguration (). Sethost ("192.168.10.150", 80); Postmethod Postmethod=NewPostmethod (TargetUrl); Httpclient.executemethod (Postmethod); Postmethod.releaseconnection (); intStatusCode =Postmethod.getstatuscode (); if(StatusCode = =httpstatus.sc_moved_permanently|| StatusCode = =httpstatus.sc_moved_temporarily|| StatusCode = =Httpstatus.sc_see_other|| StatusCode = =httpstatus.sc_temporary_redirect) {Header Responseheader= Postmethod.getresponseheader ("Location"); if(Responseheader! =NULL) {String value=Responseheader.getvalue (); System.out.println ("REDIRECT location =" +value); if(Stringutils.isnotempty (value) && value.indexof ("login")! = 1) {namevaluepair name=NewNamevaluepair ("UserName", "admin"); Namevaluepair pwd=NewNamevaluepair ("pwd", "admin"); Postmethod Loginmethod=NewPostmethod ("/pdc/userlogin.html"); Loginmethod.setrequestbody (Newnamevaluepair[]{name,pwd}); Httpclient.executemethod (Loginmethod); Loginmethod.releaseconnection (); /**methods of obtaining cookies cookie[] cookies = httpclient.getstate (). GetCookies (); System.out.println ("----------------------------cookie Start------------------------------------------"); for (Cookie cookie:cookies) {System.out.println (cookie); } System.out.println ("----------------------------------------------------------------------"); header[] headercookies = postmethod.getresponseheaders ("Set-cookie"); for (header Header:headercookies) {System.out.println (header); } System.out.println ("----------------------------Cookie End------------------------------------------" ); */GetMethod Targetmethod=NewGetMethod (TargetUrl); Httpclient.executemethod (Targetmethod); System.out.println (Targetmethod.getresponsebodyasstring ()); Targetmethod.releaseconnection (); } } Else{System.out.println ("Invalid redirect"); } } Else{System.out.println (postmethod.getresponsebodyasstring ()); } } //multi-threaded access to httpclient (the same site), only need to httpclient when instantiating the Multithreadedhttpconnectionmanager can be Public Static voidgethttpclient () {Multithreadedhttpconnectionmanager ConnectionManager=NewMultithreadedhttpconnectionmanager (); HttpClient Client=NewHttpClient (ConnectionManager); } }//for an example of uploading a file via HTTP and submitting XML format data, see the original address//http://www.blogjava.net/Alpha/archive/2007/01/22/95216.html
Version httpclient_3.x usage