How auto redirect in HttpClient (java, apache) -


i create httpclient , set settings

httpclient client = new httpclient();  client.getparams().setcookiepolicy(cookiepolicy.browser_compatibility); client.getparams().setcontentcharset("utf-8"); 

first request (get)

getmethod first = new getmethod("http://vk.com"); int returncode = client.executemethod(first);  bufferedreader br = null; string lineresult = ""; if (returncode == httpstatus.sc_not_implemented) {     system.err.println("the post method not implemented uri");     // still consume response body     first.getresponsebodyasstring(); } else {     br = new bufferedreader(new inputstreamreader(first.getresponsebodyasstream(), charset.forname("windows-1251")));     string readline = "";     while (((readline = br.readline()) != null)) {         lineresult += readline;     } } 

response correct.

second request (post):

postmethod second = new postmethod("http://login.vk.com/?act=login");  second.setrequestheader("referer", "http://vk.com/");  second.addparameter("act", "login"); second.addparameter("al_frame", "1"); second.addparameter("captcha_key", ""); second.addparameter("captcha_sid", ""); second.addparameter("expire", ""); second.addparameter("q", "1"); second.addparameter("from_host", "vk.com"); second.addparameter("email", email); second.addparameter("pass", password);  returncode = client.executemethod(second);  br = null; lineresult = ""; if (returncode == httpstatus.sc_not_implemented) {     system.err.println("the post method not implemented uri");     // still consume response body     second.getresponsebodyasstring(); } else {     br = new bufferedreader(new inputstreamreader(second.getresponsebodyasstream()));     string readline = "";     while (((readline = br.readline()) != null)) {         lineresult += readline;     } } 

this response correct too, need redirected headers.location.

i not know how value headers location or how automatically enable redirection.

due design limitations httpclient 3.x unable automatically handle redirects of entity enclosing requests such post , put. either have manually convert post request upon redirect or upgrade httpclient 4.x, can handle types of redirects automatically.


Comments