Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpWebRequest Encoding - UTF-8

I'm trying to fetch some data from a web page but i faced character problem. The web page is in utf-8 format and in multi-language, in this case, i can fetch the english based senteces/datas without any problem but for italian, spanish and turkish data, i'm getting wrong characters.

when i check the saved html file, for text coding it says : windows-1254

As you can see in my method i tried to solve the problem by using in streamreader ;

    Encoding.GetEncoding("utf-8")
    Encoding.Default
    Encoding.UTF8

Httpwebrequest :

string postdata = "username" + usern + "&pass=" + pass";
byte[] bytes = new UTF8Encoding().GetBytes(postdata);
 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.*******.com/login.php");
request.Method = "POST";
request.KeepAlive = true;
request.CookieContainer = cont;
request.AutomaticDecompression = DecompressionMethods.Deflate;
request.CookieContainer.Add(cok);
request.ContentType = "text/html; charset=utf-8";                  
request.UserAgent = "Mozilla/2.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/9.0";
request.ContentLength = bytes.Length;
request.GetRequestStream().Write(bytes, 0, bytes.Length);

HttpWebResponse response = null;
response = (HttpWebResponse)request.GetResponse();          
StreamReader _str2 = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
 string html = _str2.ReadToEnd();
File.WriteAllText("login_response.html", html);
like image 353
LikePod Avatar asked Feb 14 '26 06:02

LikePod


1 Answers

I believe your error is not on the fetching, but on the writing of the new file, so instead of using File.WriteAllText, you perhaps should look at:

How to write out a text file in C# with a code page other than utf-8?

i.e.

using (StreamWriter sw = new StreamWriter(File.Open(myfilename, FileMode.Create), Encoding.WhateverYouWant))
{    
    sw.WriteLine("my text...");     
}

(example from that page).

--- EDIT :

You can do the same on File.WriteAllText apparently: https://msdn.microsoft.com/en-us/library/ms143376(v=vs.110).aspx

like image 97
Jmons Avatar answered Feb 15 '26 19:02

Jmons



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!