c# - WebClient DownloadString UTF-8 不显示国际字符

标签 c# html encoding utf-8 webclient

我尝试将网站的 html 保存在字符串中。该网站有国际字符(ę、ś、ć、...),即使我将编码设置为与网站字符集相对应的 UTF-8,它们也不会保存到字符串中。

这是我的代码:

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

当我将“htmlCode”打印到控制台时,即使在原始 HTML 中国际字符显示正确,国际字符也未正确显示。

感谢任何帮助。

最佳答案

我遇到了同样的问题。 client.DownloadString 似乎没有使用 UTF-8 对字符进行编码。使用 client.DownloadData 并使用 Encoding.UTF8.GetString 对返回的数据进行编码可以解决问题。

using (WebClient client = new WebClient())
{
     var htmlData = client.DownloadData("http://www.filmweb.pl/Mroczne.Widmo");
     var htmlCode = Encoding.UTF8.GetString(htmlData);
}

关于c# - WebClient DownloadString UTF-8 不显示国际字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37200465/

相关文章:

c# - 如何为 Web API 中的所有数据成员将 EmitDefaultValue 全局设置为 false?

html - 有什么方法可以为超过(例如)10 个字母的单词设置函数吗?

python - Unicode解码错误: 'ascii' codec can't decode byte 0x92 in position 47: ordinal not in range(128)

php - PHP 中的字符串文字是否只能以 ASCII 的兼容超集(例如 UTF-8 或 ISO-8859-1)的编码进行编码?

python - 从文本文件中删除不可打印的 "gremlin"字符

c# - Parallel.ForEach 与 HttpClient 和 ContinueWith

c# - 比较 Linq 查询中的 byte[]

c# - 如何在C#中模拟鼠标点击? (我一直收到错误)

javascript - 点击功能中的 sencha touch HTMLDivElement

javascript - 将动态创建的元素的数据属性与 JQuery 进行比较