将HttpMethod的内容复制到另一个HttpMethod
问题描述:
我一直试图在Java代码中处理重定向(302),现在我完成了我的代码。我遇到了另一个问题,在登录后点击我注销的任何链接。所以我通过wireshark检查了我的TCP流,发现缺少几个HeaderRequests
。实现我的代码后,HTTP头如下:将HttpMethod的内容复制到另一个HttpMethod
GET /index.php/ HTTP/1.1
Host: 10.28.161.31
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111109 CentOS/3.6-3.el5.centos Firefox/3.6.24
Cookie: PHPSESSID=d488eea5e85afc8ec526c1a749e7ab20; path=/
Referrer: http://10.28.161.31
Cookie: $Version=0; PHPSESSID=d488eea5e85afc8ec526c1a749e7ab20; $Path=/ ???
和原始HTTP头如下:
GET/HTTP/1.1
Host: 10.28.161.31
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111109 CentOS/3.6-3.el5.centos Firefox/3.6.24
Referer: http://10.28.161.31/index.php
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Cookie: PHPSESSID=978ee1e3b3696743c5c8f507a2ec7212
据我的观察,我没有正确复制的标题的内容,这就是为什么它正在快速注销。所以我的问题是,如何将HttpMethod
的完整内容复制到另一个HttpMethod
?如果任何人可以提供代码片段,或者示例/教程会很棒,或者如果任何人能够提醒我我做错事的地方,那将是可观的。
我的实现就在这里:
private HttpMethod loadHttp302Request(HttpMethod method, HttpClient client,
int status, String urlString) throws HttpException, IOException {
if (status!=302)
return null;
String[] url = urlString.split("/");
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);
return theMethod;
}
请仔细阅读这篇文章,究竟如何做一个翻译黑/白HttpClient和DefaultHttpClient? – Ahmed 2011-12-30 00:47:23
不适用于我.. – Ahmed 2011-12-30 19:34:44