无法找到此特定站点的位置字段 "http://www.macys.com/"

Not able to find location field for this particular site "http://www.macys.com/"

我有以下站点:“http://www.macys.com/”有重定向。

这是代码片段:

String location = "http://www.macys.com/";
do {
    HttpURLConnection conn = null;
    try {
        url = location;
        conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestMethod("GET");
        /**
         * Setting the Browser properties
         */
        conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76 Safari/537.36");
        conn.setConnectTimeout(2000);
        conn.setInstanceFollowRedirects(false);
        conn.setReadTimeout(10000);
        conn.connect();
    } catch (Exception e) {
        logger.error("Exception : {}", e.getMessage());
    }

    responseCode = connection.getResponseCode();

    if ((responseCode > 300 && responseCode < 310)) {
        location = connection.getHeaderField("Location");
        if (location == null)
            break;
    }
} while (responseCode > 300 && responseCode < 310);

第一次迭代后,响应代码为302,但未找到位置字段。

我做错了什么?

如果您查看响应 body,您会发现该站点拒绝了您的请求。

Access Denied: You don't have permission to access the requested URL on this server.

该网站不会向您发送 Location header,尽管当他们拒绝您访问时显示响应代码为 302。