B. Ulaş Şenol
B. Ulaş Şenol

Reputation: 91

StreamReader Turkish Character Encoding

How can I fix this problem?

string icerik;

WebRequest istek = HttpWebRequest.Create(adres);
istek.Proxy = null;
WebResponse cevap = istek.GetResponse();

CultureInfo tr = new CultureInfo("tr-TR");

StreamReader gelenBilgi = new StreamReader(cevap.GetResponseStream(), Encoding.GetEncoding(tr.TextInfo.ANSICodePage));
icerik = gelenBilgi.ReadToEnd();

htmlDoc.LoadHtml(icerik);

I tried some methods to solve the problem but didn't work. For example;

StreamReader gelenBilgi = new StreamReader(cevap.GetResponseStream(), Encoding.GetEncoding("iso-8859-9"));

or

StreamReader gelenBilgi = new StreamReader(cevap.GetResponseStream(), Encoding.GetEncoding("windows-1254"));

or

StreamReader gelenBilgi = new StreamReader(cevap.GetResponseStream(), Encoding.UTF8);

Upvotes: 0

Views: 1532

Answers (1)

Bora Karaca
Bora Karaca

Reputation: 511

I was having the same problem. This method helped me solve the Turkish character encoding.

            string content;
            using (var sr = new StreamReader(cevap.GetResponseStream()))
            {
                Encoding iso = Encoding.GetEncoding("iso-8859-9");
                Encoding utf8 = Encoding.UTF8;
                byte[] utfBytes = utf8.GetBytes(sr.ReadToEnd());
                byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
                content = iso.GetString(isoBytes);
            }

Upvotes: 2

Related Questions