0

I am using Visual Studio in C# (.Net 4.5). I made a simple program that has a single button. When I click it, it fetches the first five results of a Google query on Justin Trudeau, some of them in french, hence the "fr" in the query string. It then displays those results in a richTextBox, in a webBrowser, and in a MessageBox. But it doesn't display the french characters correctly. Why? Here's an image of the result: http://richardlatulippe.webs.com/result.png

And here is the code behind the button : ` (using System.IO;)

WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string query, html, result="", results="";
int start=0, end;
query = "https://www.google.com/search?q=justin+trudeau+fr";
html = wc.DownloadString(query);
for (int c=1; c<=5; c++)
{
  start = html.IndexOf("\"r\"><a href", start) + 5;
  start = html.IndexOf(">", start) + 1;
  end = html.IndexOf("</a", start);
  result = html.Substring(start, end-start);
  if (!result.Contains("<img"))
  {
     results += result + "<br>";
     result = result.Replace("<b>", "").Replace("</b>", "");
     richTextBox1.Text += result + "\n";
   }
 }
 webBrowser1.DocumentText = results;
 MessageBox.Show(results);`
Richard L.
  • 5
  • 1
  • 5
  • See this [Kanji characters from WebClient html different from actual Kanji](https://stackoverflow.com/questions/49846392/kanji-characters-from-webclient-html-different-from-actual-kanji-in-website?answertab=active#tab-top). – Jimi Apr 28 '18 at 02:26
  • Thanks @Jimi, that works. I wish I understood what the problem was. Is it the DownloadString method that isn't ok? – Richard L. Apr 28 '18 at 22:11
  • It's the WebClient that is not really engineered for this tasks. You are supposed to used it for downloading from known sources. Microsoft uses it for downloading its components (updating Visual Studio, for example). For less specific purposes, you have WebRequest and HttpClient. In this case, you should know what encoding will be used for a generic Html page. An HttpWebRequest doesn't have any problem with this. It will always return the correct encoding. – Jimi Apr 28 '18 at 22:19
  • Thanks again @Jimi – Richard L. Apr 29 '18 at 17:04

0 Answers0