HttpWebRequest &原生 GZip 压缩

HttpWebRequest amp; Native GZip Compression(HttpWebRequest amp;原生 GZip 压缩)
本文介绍了HttpWebRequest &原生 GZip 压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

请求使用 Gzip 压缩的页面时,我收到很多以下错误:

When requesting a page with Gzip compression I am getting a lot of the following errors:

System.IO.InvalidDataException:GZip 页脚中的 CRC 与CRC 从解压后计算出来的数据

System.IO.InvalidDataException: The CRC in GZip footer does not match the CRC calculated from the decompressed data

我正在使用本机 GZipStream 进行解压缩,并且正在考虑解决这个问题.考虑到这一点,是否有解决此问题或其他 GZip 库(免费?)可以正确处理此问题的解决方法?

I am using native GZipStream to decompress and am looking at addressing this. With that in mind is there a work around for addressing this or another GZip library (free?) which will handle this issue properly?

我正在验证 webResponse ContentEncoding 是 GZIP

I am verifying the webResponse ContentEncoding is GZIP

更新 5/11一个简化的片段

//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;
}

推荐答案

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

What about the webrequest AutomaticDecompression Property available since .net 2? Simply add:

webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

它还将 gzip,deflate 添加到接受编码标头中.

It also adds the gzip,deflate to the accept encoding header.

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

这篇关于HttpWebRequest &原生 GZip 压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Is Unpivot (Not Pivot) functionality available in Linq to SQL? How?(Linq to SQL 中是否提供 Unpivot(非 Pivot)功能?如何?)
How to know if a field is numeric in Linq To SQL(如何在 Linq To SQL 中知道字段是否为数字)
Linq2SQl eager load with multiple DataLoadOptions(具有多个 DataLoadOptions 的 Linq2SQl 急切加载)
Extract sql query from LINQ expressions(从 LINQ 表达式中提取 sql 查询)
LINQ Where in collection clause(LINQ Where in collection 子句)
Orderby() not ordering numbers correctly c#(Orderby() 没有正确排序数字 c#)