gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:31:49 27 4
gpt4 key购买 nike

我尝试将网站的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com