【发布时间】:2011-12-28 10:26:35
【问题描述】:
我一直在尝试在 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肯定显示我收到了完整的响应正文。
任何想法都将不胜感激:)
【问题讨论】:
-
在
getMethodurlString应该换成url[0]+"//"+url[2]不是吗? -
Nope... 那么你将有一个路径 = abc.com/www.abc.com