Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpWebResponse - Encoding problem

Tags:

c#

encoding

I have a problem with encoding. When I get site's source code I have: enter image description here

I set encoding to UTF8 like this:

StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string sourceCode = reader.ReadToEnd();

Thanks for your help!

like image 602
carck3r Avatar asked Dec 21 '22 15:12

carck3r


2 Answers

Try to use the encoding specified:

Encoding encoding;
try
{
    encoding = Encoding.GetEncoding(response.CharacterSet);
}
catch (ArgumentException)
{
    // Cannot determine encoding, use dafault
    encoding = Encoding.UTF8;
}

StreamReader reader = new StreamReader(response.GetResponseStream(), encoding);
string sourceCode = reader.ReadToEnd();

If you are accepting gzip somehow, this may help: (Haven't tried it myself and admittedly it doesn't make much sense since your encoding is not gzip?!)

request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
like image 55
ziya Avatar answered Jan 02 '23 19:01

ziya


I had the same issue, I tried changing encoding, from the source to the result, and I got nothing. in the end, I come across a thread that leads me to the following... Take look here... .NET: Is it possible to get HttpWebRequest to automatically decompress gzip'd responses?

you need to use the following code, before retrieving the response from the request.

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

since once we use accept-encoding 'gzip' or 'deflate', the data get compressed, and turn into data unreadable by us. so we need to decompress them.

like image 43
deadManN Avatar answered Jan 02 '23 20:01

deadManN