为什么 Java 的 HttpsURLConnection 和 curl 的 HEAD 请求 return 不同 headers?

Why Java's HttpsURLConnection and curl's HEAD requests return different headers?

我注意到当我使用 HEAD 请求时,javax.net.ssl.HttpsURLConnection 在响应中遗漏了一些 header。

这是我使用的代码:

  public static void getHeaders() throws IOException, URISyntaxException {
        URL url = new URL("https://acme-v01.api.letsencrypt.org/terms");
        HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
        con.setRequestMethod("HEAD");
        con.getInputStream().close();
        System.out.println(con.getHeaderField("Location"));
        Map<String, List<String>> map = con.getHeaderFields();
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            System.out.println("Key : " + entry.getKey() +
                    " ,Value : " + entry.getValue());
        }
    }

下一个输出:

null
Key : X-Frame-Options ,Value : [DENY]
Key : null ,Value : [HTTP/1.1 200 OK]
Key : Cache-Control ,Value : [max-age=60987]
Key : Server ,Value : [nginx]
Key : ETag ,Value : ["582cdb9c-22004"]
Key : Connection ,Value : [keep-alive]
Key : Expires ,Value : [Thu, 01 Dec 2016 14:26:25 GMT]
Key : Last-Modified ,Value : [Wed, 16 Nov 2016 22:20:12 GMT]
Key : Date ,Value : [Wed, 30 Nov 2016 21:29:58 GMT]
Key : Content-Type ,Value : [application/pdf]

我们可以看到它没有return "Location" header.

这是在同一台服务器上执行的简单 curl 请求:

# curl -I https://acme-v01.api.letsencrypt.org/terms
HTTP/1.1 302 Moved Temporarily
Server: nginx
Content-Type: text/plain; charset=utf-8
Boulder-Request-Id: JBtoTNb6Q-vvdR5VF9j3gIP27S4RmEqaLjepyF60aVU
Location: https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf
Replay-Nonce: x7jscxXZ-90jzgLG8vkmunp7XrlLrMT0R4mzFV7xAhQ
X-Frame-Options: DENY
Strict-Transport-Security: max-age=604800
Content-Length: 0
Expires: Wed, 30 Nov 2016 21:37:03 GMT
Cache-Control: max-age=0, no-cache, no-store
Pragma: no-cache
Date: Wed, 30 Nov 2016 21:37:03 GMT
Connection: keep-alive

为什么 "HEAD" 请求 return 不同的 header?

正如 zapl 在评论中所说,这是因为此 link 具有重定向。并且似乎默认情况下 HttpsURLConnection 接受重定向。但是我们可以手动关闭它:

con.setInstanceFollowRedirects(false);