我怎样才能DECOM preSS GZIP /使用HttpWebRequest的DEFLATE内容 [英] How can I decompress GZIP/DEFLATE content with HTTPWebRequest

查看:189
本文介绍了我怎样才能DECOM preSS GZIP /使用HttpWebRequest的DEFLATE内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来,阅读从Microsoft了HTTPClient(包装在3.5及以前)的响应流,或者HTTPWebResponse(.NET 4.0和更高版本)时,这两种情况都开始返回二进制内容。

It appears that when reading the response stream from either the Microsoft HTTPClient (wrapper in 3.5 and before), or the HTTPWebResponse (.NET 4.0 and later), both instances are starting to return binary content.

我们有数百个网络蜘蛛,许多使用不同的框架或一次性code的。他们是一个接一个开始一切回归垃圾的HTML。我想这是我们都在触摸和重建DLL的那些。这让我想起一些在框架发生变化。

We have hundreds of web spiders, many using different frameworks or "one-off" code. And they are one by one starting to all return junk for the HTML. I assume it's the ones we are touching and rebuilding the DLL's. Which makes me think something changed in the framework.

这是我的主要方法:

Public Function PostPage(ByVal URL As String, ByVal enc As Encoding) As String
    Try
        ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateCertificate)
        Dim htmlRequest As HttpWebRequest = DirectCast(WebRequest.Create(URL), HttpWebRequest)
        htmlRequest.Headers.Add("Accept-Encoding", "gzip, deflate")
        Dim htmlResponse As HttpWebResponse = DirectCast(htmlRequest.GetResponse(), HttpWebResponse)
        Return New System.IO.StreamReader(htmlResponse.GetResponseStream(), enc).ReadToEnd()
    Catch ex As Exception
        Console.WriteLine("Error: " & ex.Message)
    End Try
    Return ""
End Function

这是非常基本的,我不知道发生了什么事情,每一个类型的编码(包括不指定任何编码),返回二元。这是一个工作再$ P $的什么破psentation当我投入甚至 http://www.google.com 作为URL。

推荐答案

当您添加了Accept-Encoding头,你告诉web服务器:请给我一个反应,是COM pressed使用GZIP或放气。

When you're adding the Accept-Encoding header, you're telling the web server: "Please send me a response that is compressed using GZIP or DEFLATE."

Web服务器尽职尽责地返回这样的回应和你的客户得到COM pressed内容。如果你想DECOM preSS的内容,你必须这样做手工,如果你的对象不支持自动DECOM pression。

The webserver dutifully returns such a response and your client gets the compressed content. If you want to decompress that content, you must do so manually if your object does not support automatic decompression.

而不是手动添加Accept-Encoding头,而不是告诉.NET要做到这一点,并自动DECOM preSS代表你的反应。

Rather than adding the Accept-Encoding header manually, instead tell .NET to do it and to automatically decompress the response on your behalf.

htmlRequest.AutomaticDecom pression = DECOM pressionMethods.Deflate | DECOM pressionMethods.GZip;

这篇关于我怎样才能DECOM preSS GZIP /使用HttpWebRequest的DEFLATE内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆