天天看点

java 怎么做302重定向_在Java中处理重定向302

我一直在尝试处理

java代码中的重定向(302)……以及我使用的代码

org.apache.commons.httpclient.HttpClient没有声明setRedirectStrategy(),所以我必须编写自己的重定向实现:

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;

}

执行此操作后,状态代码等于200,因此看起来一切正常,但响应主体和响应流都为空.我已经能够使用wireshark来嗅探TCP流,就Wireshark而言,我从我的重定向代码中收回了整个响应主体……所以我不确定我做错了什么或者下一步要找什么…理想情况如果我可以使用setRedirectStrategy()会很好,但因为它是客户端的代码:p我使用org.apache.commons.httpclient.HttpClient卡住…

我调试了executeMethod()并发现当从输入流中读取响应时,它似乎什么也得不到,即使wireshark肯定表明我收到了完整的响应体.

任何想法,将不胜感激 :)