作者:徐青乔府_631 | 来源:互联网 | 2023-02-07 12:22
我正在制作一个简单的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。
为什么会缺少此标头?和别人一起。把头还给我!
1> jamie..:
找到了问题。
在HttpResponseMessage
由归国HttpClient
方法有两个头属性:
HttpResponseMessage.Headers
是HttpResponseHeaders
带有通用响应标头的
HttpResponseMessage.Content.Headers
是HttpContentHeaders
具有特定内容标头的Content-Type