簡體   English   中英

為什么在我的c#HttpClient響應標頭中看不到Content-Encoding:gzip響應?

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

我正在制作一個簡單的c#程序來確定服務器端壓縮是否在各種服務器上可用/啟用。 這是我下面的請求代碼。

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();
}

通過查看提琴手中的請求,我可以看到對此請求啟用了壓縮,但是我似乎無法從代碼的響應頭中提取該信息。

這些是請求和響應的完整標頭。

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的標頭:響應中缺少gzip。

為什么會缺少此標頭? 和別人一起。 把頭還給我!

找到了問題。

HttpClient方法返回的HttpResponseMessage具有兩個標頭屬性:

HttpResponseMessage.Headers是具有通用響應標頭的HttpResponseHeaders

HttpResponseMessage.Content.Headers是一個HttpContentHeaders具有諸如Content-Type的特定於內容的標頭

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM