开发者

C# Not getting proper response from HttpWebResponse. Encoding?

I'm trying to fetch some webpages using the code below:

    public static string FetchPage(string url)
    {

         HttpWebRequest req = (HttpWebReques开发者_JS百科t)WebRequest.Create(url);

        req.Method = "GET";

        req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; sv-SE; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 (.NET CLR 3.5.30729";
        req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        req.Headers.Add("Accept-Language", "sv-se,sv;q=0.8,en-us;q=0.5,en;q=0.3");
        req.Headers.Add("Accept-Encoding", "gzip,deflate");
        req.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
        req.Headers.Add("Keep-Alive", "115");
        req.Headers.Add("Cache-Control: max-age=0");
        req.AllowAutoRedirect = true;

        req.IfModifiedSince = DateTime.Now;

        using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
        {
           using (Stream resStream = resp.GetResponseStream())
           {
              StreamReader reader = new StreamReader(resStream);
              return reader.ReadToEnd();
            }
        }
    }

Some pages work (W3C, example.com) while most others I've tried do not (BBC.co.uk, CNN.com, etc). Wireshark shows that I'm getting a proper reponse.

I've tried setting the encoding of the reader to the expected encoding of the response (CNN - utf8) as well as every possible combination but I have had no luck.

What am I missing out on here?

The first bytes of my response are always "1f ef bf bd" if you're able to tell something based on that.


I suspect the most likely explanation is that you are getting compressed data and not uncompressing it. Try using a stream filter to deflate/unzip it. See Rick Strahl's blog article for more info.


Loading http://bbc.co.uk worked for me when leaving out the "Accept-Encoding" header:

req.Headers.Add("Accept-Encoding", "gzip,deflate"); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜