Java Background Simulation Login to a website, get certain permissions after further action.
Tools used:
Apache httpcomponents client version 4.3
Here is the code:
Importorg.apache.http.HttpEntity;ImportOrg.apache.http.client.methods.CloseableHttpResponse;ImportOrg.apache.http.client.methods.HttpGet;Importorg.apache.http.impl.client.CloseableHttpClient;ImportOrg.apache.http.impl.client.HttpClientBuilder;Importorg.apache.http.util.EntityUtils; Public classSimpleClient { Public voidTest ()throwsException {httpget httpget=NULL; Try{closeablehttpclient httpclient=httpclientbuilder.create (). build (); HttpGet=NewHttpGet ("www.baidu.com"); //httpget httpget = new HttpGet (urlwithparams); //Configure timeout settings for requests /*requestconfig requestconfig = Requestconfig.custom (). Setconnectionrequesttimeout (50) . Setconnecttimeout (SetSocketTimeout). build (); Httpget.setconfig (requestconfig);*/closeablehttpresponse Response=Httpclient.execute (HttpGet); System.out.println ("StatusCode" +response.getstatusline (). Getstatuscode ()); String Set_cookie= Response.getfirstheader ("Set-cookie"). GetValue (); //Print Cookie ValueSystem.out.println (Set_cookie.substring (0,set_cookie.indexof (";"))); Httpentity Entity=response.getentity (); String Jsonstr= entityutils.tostring (entity);//, "Utf-8");System.out.println (JSONSTR); Httpget.releaseconnection (); } Catch(Exception e) {e.printstacktrace (); } finally{httpget.releaseconnection (); } }}
HTTPCLIENT4 Analog Access Web site Demo Login Simple Example