2010-09-08 49 views
0

我發現HttpClient的似乎並沒有對被處理重定向的URL:HttpClient無法302此鏈接?

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(); 

我做錯了什麼?感謝

+0

如何只要'至302'是一個動詞!?! – 2010-09-08 19:29:40

回答

0

發現從HttpClient的郵件列表的答案...

谷歌不會把頭部和獲得相同的,所以用GET 302重定向和HEAD請求給一個200 OK