17

I attempt to save the html of a website in a string. The website has international characters (ę, ś, ć, ...) and they are not being saved to the string even though I set the encoding to be UTF-8 which corresponds to the websites charset.

Here is my code:

using (WebClient client = new WebClient())
{
    client.Encoding = Encoding.UTF8;
    string htmlCode = client.DownloadString("http://www.filmweb.pl/Mroczne.Widmo");
}

When I print "htmlCode" to the console, the international characters are not shown correctly even though in the original HTML they are shown correctly.

Any help is appreciated.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
mrybak3
  • 385
  • 1
  • 3
  • 14

2 Answers2

26

I had the same problem. It seems that client.DownloadString doesn’t encode the characters using UTF-8. Using client.DownloadData and encoding the returned data with Encoding.UTF8.GetString solve the problem.

using (WebClient client = new WebClient())
{
     var htmlData = client.DownloadData("http://www.filmweb.pl/Mroczne.Widmo");
     var htmlCode = Encoding.UTF8.GetString(htmlData);
}
Abbas Amiri
  • 3,074
  • 1
  • 23
  • 23
  • unfortunately this still does not work for me. It seems that even a simple string specialChar = "ĉ" does not work either, so I'm suspecting it is a problem with the string? I don't know much about encoding but thank you for the answer nonetheless --EDIT-- I think it is my printing methods, I will get back to you as this might be the correct answer you gave me. – mrybak3 May 13 '16 at 11:30
  • Yup, that totally worked, thank you! I was just going off of the console output when in the form it was displaying correctly. Weird. Anyways, thank you! – mrybak3 May 13 '16 at 11:39
0

You're doing it the hard way. There is an Encoding property on the WebClient, which can be set to Encoding.UTF8

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 05 '22 at 05:36
  • The OP is trying that in the question already. – General Grievance Mar 09 '22 at 04:31