3

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);
LikePod
  • 300
  • 1
  • 4
  • 17

1 Answers1

1

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

Community
  • 1
  • 1
Jmons
  • 1,766
  • 17
  • 28
  • 1
    Yes, you are totally right.i was focused too much on request and response parts. thank you again. – LikePod Aug 21 '15 at 16:21