Can't load html string using HttpWebRequest / HttpWebResponse

i using HttpWebRequest / HttpWebResponse to get the html document, the following code was executed, but I cannot encode the resulting stream into an 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 am using the line of code below to check if there is a successful request.

         Console.Write(webresponse.StatusCode);

      

The result on the screen was ok, that means the request was sent, but the Response line on the screen was not html format, it was as weird as this: @ 32u% & $ & (@ * # Eeeuw

+3


source to share


1 answer


By using webrequest.Headers.Add("Accept-Encoding", "gzip, deflate");

, you are telling the server that you understand the condensed responses. Remove that header and use the default UTF8 encoding instead of the 1252 you are using. Then you should get the line you want. You can just use System.Text.Encoding.UTF8

.



+2


source







All Articles