【问题标题】:StreamReader returning symbols (using HttpWebRequest)StreamReader 返回符号(使用 HttpWebRequest)
【发布时间】:2014-08-24 12:54:21
【问题描述】:

我正在尝试从网页中获取 HTML。我已经获取并设置了 cookie。 这是请求头的一部分:

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: <.......URL......>
Cookie: ASP.NET_SessionId=rk2tt31jgxyvszna1slzthho; .ASPXAUT=<.....token......>
Connection: keep-alive

这是 Live HTTP Headers 给出的良好响应的转储标头:

HTTP/1.1 200 OK
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Expires: -1
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sun, 24 Aug 2014 12:48:36 GMT
Content-Length: 13400

我正在使用StreamReader(response.GetResponseStream(),Encoding.UTF8) 从流中读取数据。

问题是StreamReader.ReadToEnd()返回了很多符号而不是HTML代码:

�\b\0\0\0\0\0\0�\a`I�%&/m�{J�J��t

【问题讨论】:

    标签: html asp.net streamreader httpwebresponse


    【解决方案1】:

    答案是我需要解压缩响应。

    因为它是使用 GZIP 压缩的,所以我使用这种方法解压它:

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

    详情:.NET: Is it possible to get HttpWebRequest to automatically decompress gzip'd responses?

    Tks.

    【讨论】:

      猜你喜欢
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-04
      • 1970-01-01
      • 1970-01-01
      • 2013-08-07
      相关资源
      最近更新 更多