HttpClient 无法 302 此链接?

发布于 2024-09-18 07:01:53 字数 2138 浏览 5 评论 0原文

我发现 httpclient 似乎没有处理重定向的网址:

http://news.google.com/news/url?sa=t&fd= R&usg=AFQjCNGrJk-F7Dmshmtze2yhifxRsv8sRg&url=http://www.mtv.com/news/articles/1647243/20100907/story.jhtml

应该 302 到: http://www.mtv.com/news/articles/1647243/ 20100907/story.jhtml

当我查看浏览器中的标题时,一切看起来都很好:

HTTP/1.1 302 Moved Temporarily
Content-Type: text/html; charset=UTF-8
Location: http://www.mtv.com/news/articles/1647243/20100907/story.jhtml
Content-Length: 258
Date: Wed, 08 Sep 2010 18:40:21 GMT
Expires: Wed, 08 Sep 2010 18:40:21 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block
Server: GSE
Set-Cookie: PREF=ID=024209255b405b06:TM=1283971221:LM=1283971221:S=AG-13_7Cjg_EqlRY; expires=Fri, 07-Sep-2012 18:40:21 GMT; path=/; domain=.google.com
Connection: close

但是 httpclient 似乎没有给我最终的 URL。这是我正在使用的代码

HttpHead httpget = null;
HttpHost target = null;
HttpUriRequest req = null;

String startURL = "http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNGrJk-F7Dmshmtze2yhifxRsv8sRg&url=http://www.mtv.com/news/articles/1647243/20100907/story.jhtml";
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE,HttpClientFetcher.emptyCookieStore);
httpget = new HttpHead(startURL);

HttpResponse response = httpClient.execute(httpget, localContext);

Header[] test = response.getAllHeaders();
for(Header h: test) {
logger.info(h.getName()+ ": "+h.getValue());
}

target = (HttpHost) localContext.getAttribute( ExecutionContext.HTTP_TARGET_HOST );

req = (HttpUriRequest) localContext.getAttribute( ExecutionContext.HTTP_REQUEST );

// STILL PRINTS OUT THE GOOGLE NEWS LINK
finalURL = target+""+req.getURI();

我做错了什么吗?谢谢

I found a URL that httpclient doesn't seem to be handling redirects on:

http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNGrJk-F7Dmshmtze2yhifxRsv8sRg&url=http://www.mtv.com/news/articles/1647243/20100907/story.jhtml

should 302 to:
http://www.mtv.com/news/articles/1647243/20100907/story.jhtml

when I look at the headers in the browser everything looks good:

HTTP/1.1 302 Moved Temporarily
Content-Type: text/html; charset=UTF-8
Location: http://www.mtv.com/news/articles/1647243/20100907/story.jhtml
Content-Length: 258
Date: Wed, 08 Sep 2010 18:40:21 GMT
Expires: Wed, 08 Sep 2010 18:40:21 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block
Server: GSE
Set-Cookie: PREF=ID=024209255b405b06:TM=1283971221:LM=1283971221:S=AG-13_7Cjg_EqlRY; expires=Fri, 07-Sep-2012 18:40:21 GMT; path=/; domain=.google.com
Connection: close

However httpclient doesn't seem to give me the final URL. Here is the code I was using

HttpHead httpget = null;
HttpHost target = null;
HttpUriRequest req = null;

String startURL = "http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNGrJk-F7Dmshmtze2yhifxRsv8sRg&url=http://www.mtv.com/news/articles/1647243/20100907/story.jhtml";
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE,HttpClientFetcher.emptyCookieStore);
httpget = new HttpHead(startURL);

HttpResponse response = httpClient.execute(httpget, localContext);

Header[] test = response.getAllHeaders();
for(Header h: test) {
logger.info(h.getName()+ ": "+h.getValue());
}

target = (HttpHost) localContext.getAttribute( ExecutionContext.HTTP_TARGET_HOST );

req = (HttpUriRequest) localContext.getAttribute( ExecutionContext.HTTP_REQUEST );

// STILL PRINTS OUT THE GOOGLE NEWS LINK
finalURL = target+""+req.getURI();

Am I doing something wrong? thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2024-09-25 07:01:53

从 httpclient 邮件列表中找到了答案...

Google 不会将 HEAD 和 GET 视为相同,因此 GET 使用 302 重定向,而 HEAD 请求给出 200 OK

Found the answer from the httpclient mailing list...

Google doesn't treat a HEAD and GET the same, so the GET redirects with 302 and the HEAD request gives a 200 OK

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文