我正在使用http
客户端发出HttpWebRequest
请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestString);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Method = "GET";
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
public void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebResponse response = null;
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Console.WriteLine(response.Headers["Content-Encoding"]);
if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Moved)
{
Stream inputStream = response.GetResponseStream();
StreamReader stream = new StreamReader(inputStream);
string responseString = stream.ReadToEnd();
}
}
在Apache
设置下,gzip
压缩已启用并正常工作,因为从chrome浏览器向同一url
请求时,我可以在响应标题中看到content-encoding
。请参阅所附的屏幕截图。
但是当我使用上面提供的代码Content-Encoding
发出相同的请求时,标头中存在键,但其值为空字符串。
为什么我的响应标题中缺少content-encoding
值?
这是响应头字符串
Headers = {Connection: keep-alive Vary: Accept-Encoding Content-Encoding: Access-Control-Allow-Origin: * Content-Length: 36954 Content-Type: text/html; charset=UTF-8 Date: Fri, 18 Sep 2020 11:31:25 GMT Server: nginx/1.10.2 X-Powered-By: PHP/5.3.3 }
答案 0 :(得分:0)
如果其他任何人遇到相同的问题。 就我而言,问题就在这里
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
我将其更改为
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
这对我来说很奇怪,因为我的项目.Net版本是4.7.1 但是现在它可以正常工作了。