HttpWebRequest和Native GZip压缩

Pat*_*Pat 59 .net c# gzip stream http-compression

当请求具有Gzip压缩的页面时,我收到了很多以下错误:

System.IO.InvalidDataException:GZip页脚中的CRC与从解压缩数据计算的CRC不匹配

我正在使用本机GZipStream进行解压缩,正在寻找解决此问题的方法.考虑到这一点,是否有解决这个或另一个GZip库(免费?)的工作,它将正确处理这个问题?

我正在验证webResponse ContentEncoding是否为GZIP

更新5/11 简化的snippit

//Caller
public void SOSampleGet(string url) 
{
    // Initialize the WebRequest.
    webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Method = WebRequestMethods.Http.Get;
    webRequest.KeepAlive = true;
    webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
    webRequest.Referer = WebUtil.GetDomain(url);

    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();    

    using (Stream stream = GetStreamForResponse(webResponse, READTIMEOUT_CONST))
    {
        //use stream
    }
}

//Method
private static Stream GetStreamForResponse(HttpWebResponse webResponse, int readTimeOut)
{
    Stream stream;
    switch (webResponse.ContentEncoding.ToUpperInvariant())
    {
        case "GZIP":
            stream = new GZipStream(webResponse.GetResponseStream(), CompressionMode.Decompress);
            break;
        case "DEFLATE":
            stream = new DeflateStream(webResponse.GetResponseStream(), CompressionMode.Decompress);
            break;

        default:
            stream = webResponse.GetResponseStream();
            stream.ReadTimeout = readTimeOut;
            break;
        }    
    return stream;
}
Run Code Online (Sandbox Code Playgroud)

Eug*_*ene 128

自.net 2以来可用的webrequest AutomaticDecompression属性怎么样?只需添加:

webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
Run Code Online (Sandbox Code Playgroud)

它还将gzip,deflate添加到accept编码头.

请参阅http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.automaticdecompression.aspx

  • @MartinKearn,你做`HttpClientHandler handler = new HttpClientHandler(); handler.AutomaticDecompression = System.Net.DecompressionMethods.GZip | 减压方法.放气; _client = new HttpClient(handler);`见http://stackoverflow.com/questions/20990601/decompressing-gzip-stream-from-httpclient-response 我相信它需要.net 4.5。 (3认同)

rob*_*pim 7

对于 .NET Core,事情要复杂一些。需要AGZipStream是因为 没有属性(截至撰写本文时)AutomaticCompression。在这里查看我的答案:https ://stackoverflow.com/a/44508724/2421277

答案中的代码:

var req = WebRequest.CreateHttp(uri);

/*
 * Headers
 */
req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";

/*
 * Execute
 */
try
{
    using (var resp = await req.GetResponseAsync())
    {
        using (var str = resp.GetResponseStream())
        using (var gsr = new GZipStream(str, CompressionMode.Decompress))
        using (var sr = new StreamReader(gsr))

        {
            string s = await sr.ReadToEndAsync();  
        }
    }
}
catch (WebException ex)
{
    using (HttpWebResponse response = (HttpWebResponse)ex.Response)
    {
        using (StreamReader sr = new StreamReader(response.GetResponseStream()))
        {
            string respStr = sr.ReadToEnd();
            int statusCode = (int)response.StatusCode;

            string errorMsh = $"Request ({url}) failed ({statusCode}) on, with error: {respStr}";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


And*_*mar -2

本机 GZipStream 可以读取压缩的 GZIP ( RFC 1952 ) 流,但它无法处理 ZIP 文件格式。

来自http://www.geekpedia.com/tutorial190_Zipping-files-using-GZipStream.html

与第 3 方产品相比,使用 GZipStream 类的缺点是它的功能有限。限制之一是您无法为放置在存档中的文件命名。当 GZipStream 将文件压缩为 ZIP 存档时,它会从该文件中获取字节序列,并使用压缩算法来创建较小的字节序列。新的字节序列将放入新的 ZIP 文件中。当您打开 ZIP 文件时,您将打开存档文件本身;最流行的 ZIP 提取器(WinZip、WinRar 等)会将 ZIP 的内容显示为与存档本身具有相同内容的文件。


编辑:上面的注释是不正确的。GZipStream 不生成 ZIP 文件。它不是“单个文件 ZIP 流”。它是一个 GZIP 流。它们是不同的东西。无法保证处理 ZIP 存档的工具能够处理 .gz 文件。


对于可以读取 ZIP 存档(而不是单文件 ZIP 流)的实现,请尝试#ziplib(SharpZipLib,以前称为 NZipLib)