页眉中没有重定向网址。如何遵循重定向。 HttpClient 4

时间:2012-09-03 15:01:32

标签: java redirect header httpclient

我正在尝试测试以下页面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,但标题中未显示。

我的应用会检查网页网址状态',这是遵循此重定向的最佳/最快方式,并检查它是否以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.

2 个答案:

答案 0 :(得分:1)

当您的请求与浏览器发出的请求相同时,您可以保证获得相同的响应。我会尝试检查浏览器发送的其他标头,看看是否可以让你的java应用程序发送相同的标题。

答案 1 :(得分:0)

似乎fiddler2使用HttpGet请求并只读取标题。因此,通过更改我的代码中的内容,我能够得到正在寻找的正确答案。