2012-09-03 48 views
-1

我试图测试如果页面http://support.xbox.com/en-US/xbox-360/console-freeze/screen-freeze-wizard。该页面重定向到http://support.xbox.com/en-US/xbox-360/console-freeze/screen-freeze-solution,但是不会在标题中显示。页眉中没有重定向网址。如何关注重定向。 HttpClient 4

我的应用程序检查网页网址状态,什么是遵循这个重定向并检查它是否在404或200

编辑结束了最好的/最快的方式:当我使用fiddler2检查网页标题我得到以下内容:

HTTP/1.1 301 Moved Permanently 
Cache-Control: no-cache 
Pragma: no-cache 
Content-Type: text/html 
Expires: -1 
Location: /en-US/xbox-360/console-freeze/screen-freeze-solution 
X-UA-Compatible: IE=edge 
X-Content-Type-Options: nosniff 
Content-Length: 0 
Vary: Accept-Encoding 
Date: Mon, 03 Sep 2012 15:14:16 GMT 
Connection: keep-alive 

哪给出重定向。我的代码如下

public void status() { 

    HttpClient hc = new DefaultHttpClient(); 
    HttpHead hh = new HttpHead("http://support.xbox.com/en-US/xbox-360/console-freeze/screen-freeze-wizard"); 
    System.out.println("Requesting : " + hh.getURI()); 

    try { 
     HttpResponse response = hc.execute(hh); 
     System.out.println("Protocol : " + response.getProtocolVersion()); 
     System.out.println("Status code : " + response.getStatusLine().getStatusCode()); 

     Header [] headers = response.getAllHeaders(); 
     for (Header header : headers) { 
      System.out.println(" --> " + header.getName() + ":" + header.getValue()); 
     } 

    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 


} 

但它只提供了以下回应:

Requesting : http://support.xbox.com/en-US/xbox-360/console-freeze/screen-freeze-wizard 
Protocol : HTTP/1.1 
Status code : 404 
--> Cache-Control:private 
--> Content-Length:0 
--> X-Content-Type-Options:nosniff 
--> Date:Mon, 03 Sep 2012 15:23:59 GMT 
--> Connection:keep-alive 
...complete. 

回答

1

当你的要求是相同的,您的浏览器发出请求,你能保证你会得到相同的响应。我会尝试检查浏览器发送的其他头文件,并查看是否可以让您的Java应用程序发送相同的头文件。

0

看来fiddler2使用HttpGet请求,只是读取标题。所以通过在我的代码中进行更改,我能够得到正在寻找的正确响应。