3
votes

i using HttpWebRequest/HttpWebResponse to get html document, the code follow was running but i can not encode received stream to html string:

        string uri = "https://myfavoritesite.come";
        HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
        webrequest.KeepAlive = true;
        webrequest.Method = "GET";
        webrequest.ContentType = "text/html";
        webrequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        //webrequest.Connection = "keep-alive";
        webrequest.Host = "cat.sabresonicweb.com";
        webrequest.Headers.Add("Accept-Encoding", "gzip, deflate");
        webrequest.Headers.Add("Accept-Language", "en-US,en;q=0.5");
        webrequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0";

        HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();

        Console.Write(webresponse.StatusCode);
        Stream receiveStream = webresponse.GetResponseStream();


        Encoding enc = System.Text.Encoding.GetEncoding(1252);//1252
        StreamReader loResponseStream = new
          StreamReader(receiveStream, enc);

        string Response = loResponseStream.ReadToEnd();

        loResponseStream.Close();
        webresponse.Close();

        Console.Write(Response);

So, i use below code line to test is there successful request.

         Console.Write(webresponse.StatusCode);

The result on the screen was OK, it's mean the request was sent but the Response string expose on screen was not html format, it's something so strange like this: @32u%&$&(@*#Eeeuw

1

1 Answers

2
votes

By using webrequest.Headers.Add("Accept-Encoding", "gzip, deflate"); you are telling the server that you understand compressed responses. Remove that header and use a normal UTF8 encoding instead of 1252 that you are using. You should then get the proper string. You can just use System.Text.Encoding.UTF8.