Processing redirection 302 in Java
I've been trying to handle redirection in Java code (302)... And the code I use
private void loadHttp302Request(HttpMethod method,HttpClient client,int status,String urlString) throws HttpException,IOException { if (status!=302) return; String[] url = urlString.split("/"); logger.debug("Method -> loadHttp302Request -> Location : " + method.getResponseHeader("Location") .getValue()); logger.debug("Method -> loadHttp302Request -> Cookies : " + method.getResponseHeader("Set-Cookie") .getValue()); logger.debug("Method -> loadHttp302Request -> Referrer : " + url[0]+"//"+url[2]); HttpMethod theMethod = new getmethod(urlString+method.getResponseHeader("Location") .getValue()); theMethod.setRequestHeader("Cookie",method.getResponseHeader("Set-Cookie") .getValue()); theMethod.setRequestHeader("Referrer",url[0]+"//"+url[2]); int _status = client.executeMethod(theMethod); logger.debug("Method -> loadHttp302Request -> Status : " + _status); method = theMethod; }
After this operation, the status code equals 200, so everything looks normal, but the response body and response flow are empty I have been able to use Wireshark to sniff TCP streams. As far as Wireshark is concerned, I retrieved the entire response body from my redirection code... So I'm not sure what I did wrong or what to look for next... Ideally, it would be good if I could use setredirectstrategy(), but because it is the client Code: P I use org apache. commons. httpclient. Httpclient stuck
I debugged ExecuteMethod () and found that when I read the response from the input stream, it seemed to get nothing, even though Wireshark must indicate that I received the complete response body
Any ideas will be appreciated:)
Solution
method = theMethod; Nothing useful will be done at the end of the loadhttp302request When loadhttp302request returns, the method object pointer in the calling (Java) method will still point to the original httpmethod object
Return the method from loadhttp302request and get the response content from it