StreamReader returns characters (using HttpWebRequest)
I am trying to get HTML from a webpage. I have already installed and set cookies. Here is a snippet of the request header:
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
Here's a dump header with a nice response given by 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
I am using StreamReader(response.GetResponseStream(),Encoding.UTF8)
to read from a stream.
The problem is that it StreamReader.ReadToEnd()
returns a lot of characters instead of HTML:
\ B \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ a`I% & / m {JJT
+3
source to share
1 answer
The answer is that I needed to unzip the answer.
Since it was archived using GZIP, I unpacked it using this method:
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
Details: .NET: is it possible for HttpWebRequest to automatically unpack gzip'd responses?
Tks.
+2
source to share