C# WebClient下载UTF8格式的字符串,不返回法语字符
我正在使用C#(.NET4.5)中的Visual Studio。 我做了一个简单的程序,只有一个按钮。当我点击它时,它会获取Google查询Justin Trudeau的前五个结果,其中一些是法语,因此查询字符串中有“fr”。然后,它将这些结果显示在richTextBox、webBrowser和MessageBox中。 但是它不能正确显示法语字符。为什么? 以下是结果的图像: 下面是按钮后面的代码: ` (使用System.IO;)C# WebClient下载UTF8格式的字符串,不返回法语字符,c#,webclient,french,C#,Webclient,French,我正在使用C#(.NET4.5)中的Visual Studio。 我做了一个简单的程序,只有一个按钮。当我点击它时,它会获取Google查询Justin Trudeau的前五个结果,其中一些是法语,因此查询字符串中有“fr”。然后,它将这些结果显示在richTextBox、webBrowser和MessageBox中。 但是它不能正确显示法语字符。为什么? 以下是结果的图像: 下面是按钮后面的代码: ` (使用System.IO;) WebClient wc=新的WebClient(); wc.
WebClient wc=新的WebClient();
wc.Encoding=Encoding.UTF8;
字符串查询,html,结果=”,结果=”;
int start=0,结束;
查询=”https://www.google.com/search?q=justin+特鲁多+fr”;
html=wc.DownloadString(查询);
对于(int c=1;cSee this。谢谢@Jimi,这很有效。我希望我能理解问题所在。是DownloadString方法不正确吗?是WebClient没有真正用于此任务。您应该使用它从已知来源下载。Microsoft使用它来下载其组件(例如,更新Visual Studio)。出于不太具体的目的,您有WebRequest和HttpClient。在这种情况下,您应该知道通用Html页面将使用什么编码。HttpWebRequest对此没有任何问题。它将始终返回正确的编码。再次感谢@Jimi
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);`