Background: Login with HttpClient POST request, redirect login required, request redirected address
In HttpClient, the POST request does not have to be redirected like a GET request, and the way to do this is to determine if the POST request return code is 302, and if so, to get the address of the location to be passed over, to splice, to make a GET request
Implementation Code
Public map<string, string> doPost (string url, map<string, string> Map, String charset) {HttpClient HttpClient = NULL; HttpPost httppost = null; String result = null; String domain = "http://user.hqygou.com"; map<string, string> returnmap = new hashmap<string, string> (); try {httpClient = new sslclient (); httppost = new HttpPost (URL);//Set parameter list<namevaluepair> list = new arraylist<namevaluepair> (); Iterator Iterator = Map.entryset (). iterator (); while (Iterator.hasnext ()) {entry<string, string> elem = (entry<string, string> ) Iterator.next (); List.add (New Basicnamevaluepair (Elem.getkey (), Elem.getvalue ())); SYSTEM.OUT.PRINTLN ("The requested parameter is:" + elem.getkey () + ":" + elem.getvalue ());} if (list.size () > 0) {urlencodedformentity entity = new Urlencodedformentity (list, CharSet); Httppost.setentity ( entity);} Set Header information Httppost.setheader ("Content-type", "application/x-www-form-urlencoded; Charset=utf-8 "); HttpResponse response = Httpclient.execute (HttpPost), if (resPonse = null) {int code = Response.getstatusline (). Getstatuscode (); System.out.println ("Return code:" + code), if (code = = 302) {#判断post的请求返回码Header [] hr = Response.getallheaders (); for (int i = 0; i < hr.length; i++) {Header header1 = hr[i]; System.out.println ("All contents of the head:" + header1);} String Hearder = response.getheaders ("location") [0].tostring (). Split (":") [1].trim (); #获取返回码中头部中location is the redirected address string redirecturl = domain + hearder; Need and Domain name stitching System.out.println ("Start redirect, Address:" + redirecturl); cookies = Response.getheaders ("Set-cookie") [0].tostring ( ). Split (":") [1].trim (); SYSTEM.OUT.PRINTLN ("Acquired Cookie:" + cookies); cookies = Cookies.split (";") [0].trim (); HttpGet (RedirectURL, cookies); #get请求, make a concatenation of the acquired cookie} else {httpentity resentity = response.getentity (); if (resentity! = null) {result = ENTITYUTILS.T Ostring (Resentity, CharSet);}} Returnmap.put ("content", result); Returnmap.put ("Cookies", Cookies);}} catch (Exception ex) {ex.printstacktrace ();} return returnmap;}Run the Portal
public static void Main (string[] args) {Test post = new test (); String url = "Http://xxx/login/index/checklogin"; map<string, string> map = new hashmap<string, string> (), Map.put ("from", "XX"), Map.put ("username", "xx"); Map.put ("Password", "xx");p ost.dopost (URL, map, "UTF-8");}
Note, the back of this 200, is the content returned when the GET request, GET request can view another article, http://www.cnblogs.com/chongyou/p/7808035.html
HttpClient in POST request redirection