为什么在我的 c# HttpClient 响应 header 中看不到 Content-Encoding: gzip 响应?

Why cant I see the Content-Encoding: gzip response in my c# HttpClient response header?

我正在制作一个简单的 c# 程序来确定服务器端压缩在各种服务器上是否 available/enabled。这是我下面的请求代码。

using (var client = new HttpClient())
{
    client.Timeout = new TimeSpan(0, 0, 5);
    client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
    client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36");
    client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, sdch, br");
    var httpClientResponse = client.GetAsync(websiteURL).Result;
    string header = httpClientResponse.Headers.GetValues("Content-Encoding").First();
}

通过观察 fiddler 中的请求,我可以看到该请求已启用压缩,但是我似乎无法从代码中的响应 header 中提取该信息。

这些是完整的 header 请求和响应。

GET https://www.dobbies.com/ HTTP/1.1
Accept: text/html, application/xhtml+xml, application/xml; q=0.9, image/webp, */*; q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36
Accept-Encoding: gzip, deflate, sdch, br
Host: www.dobbies.com

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: 
Set-Cookie: ASP.NET_SessionId=hx1rb34ottgfritgt3rciql4; path=/; secure; HttpOnly
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Strict-Transport-Security: max-age=31536000
X-Xss-Protection: 1; mode=block
Date: Fri, 07 Apr 2017 08:06:17 GMT
Content-Length: 16836

这是我使用httpClientResponse.Headers

时得到的结果
{
    Vary: Accept-Encoding
    X-Frame-Options: SAMEORIGIN
    X-Content-Type-Options: nosniff
    Strict-Transport-Security: max-age=31536000
    X-Xss-Protection: 1; mode=block
    Cache-Control: private
    Date: Fri, 07 Apr 2017 08:06:17 GMT
    Set-Cookie: ASP.NET_SessionId=hx1rb34ottgfritgt3rciql4; path=/; secure; HttpOnly
    Server: 
}

如您所见,Content-Encoding 的 header:响应中缺少 gzip。

为什么 header 会丢失?和其他人一起。把我的 header 还给我!

找到问题了。

HttpClient 方法返回的 HttpResponseMessage 有两个 header 属性:

HttpResponseMessage.Headers 是具有通用响应 headers

HttpResponseHeaders

HttpResponseMessage.Content.HeadersHttpContentHeaders 和 content-specific header 类似 Content-Type