最简单的方法是对该类使用内置的自动解压缩功能
HttpWebRequest。
var request = (HttpWebRequest)HttpWebRequest.Create("http://stackoverflow.com");request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
为此,
WebClient您必须使自己的类派生
WebClient并覆盖该
GetWebRequest()方法。
public class GZipWebClient : WebClient{ protected override WebRequest GetWebRequest(Uri address) { HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address); request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; return request; }}
另请参见此SO线程:.NET的HttpWebResponse是否自动解压缩GZiped和Deflated响应?
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)