2.1.1. C#中自动处理返回的压缩的HTML

当你处理http时,返回的html的header中包含:

Content-Encoding: deflate

Content-Encoding: gzip

时,说明是返回的是压缩的HTML。

想要支持压缩的html的话,C#中,在发送HttpWebRequest之前,设置AutomaticDecompression为对应的参数即可:


HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
 
......
 
req.Headers["Accept-Encoding"] = "gzip, deflate";
//req.AutomaticDecompression = DecompressionMethods.GZip;
req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

        

更多解释详见:

【已解决】C#中HttpWebRequest使用Proxy后异常