WebClient.DownloadString 由于编码问题导致字符损坏,但浏览器正常

发布于 2024-11-30 18:44:16 字数 812 浏览 1 评论 0原文

以下代码:

var text = (new WebClient()).DownloadString("http://export.arxiv.org/api/query?search_query=au:Freidel_L*&start=0&max_results=20"));

生成一个变量 text,其中包含字符串等

“$$-Minkowski 空间、标量场和洛伦兹不变性问题”

但是,当我在 Firefox 中访问该 URL 时,我得到

$κ$-闵可夫斯基空间、标量场和洛伦兹不变性问题

实际上是正确的。我也尝试过,

var data = (new WebClient()).DownloadData("http://export.arxiv.org/api/query?search_query=au:Freidel_L*&start=0&max_results=20");
var text = System.Text.UTF8Encoding.Default.GetString(data);

但这给出了同样的问题。

我不确定这里的错在哪里。提要是否谎称是 UTF8 编码的,并且浏览器足够聪明,可以识别出这一点,但 WebClient 却不能?提要是否已正确进行 UTF8 编码,但 WebClient 因其他原因而失败?我可以采取什么措施来缓解这种情况?

The following code:

var text = (new WebClient()).DownloadString("http://export.arxiv.org/api/query?search_query=au:Freidel_L*&start=0&max_results=20"));

results in a variable text that contains, among many other things, the string

"$κ$-Minkowski space, scalar field, and the issue of Lorentz invariance"

However, when I visit that URL in Firefox, I get

$κ$-Minkowski space, scalar field, and the issue of Lorentz invariance

which is actually correct. I also tried

var data = (new WebClient()).DownloadData("http://export.arxiv.org/api/query?search_query=au:Freidel_L*&start=0&max_results=20");
var text = System.Text.UTF8Encoding.Default.GetString(data);

but this gave the same problem.

I'm not sure where the fault lies here. Is the feed lying about being UTF8-encoded, and the browser is smart enough to figure that out, but not WebClient? Is the feed properly UTF8-encoded, but WebClient is failing in some other way? What can I do to mitigate this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的故事 2024-12-07 18:44:16

这不是说谎。在调用 DownloadString 之前,您应该先设置 Web 客户端的编码。

using(WebClient webClient = new WebClient())
{
webClient.Encoding = Encoding.UTF8;
string s = webClient.DownloadString("http://export.arxiv.org/api/query?search_query=au:Freidel_L*&start=0&max_results=20");
}

至于为什么你的替代方案不起作用,那是因为用法不正确。它应该是:

System.Text.Encoding.UTF8.GetString()

It's not lying. You should set the webclient's encoding first before calling DownloadString.

using(WebClient webClient = new WebClient())
{
webClient.Encoding = Encoding.UTF8;
string s = webClient.DownloadString("http://export.arxiv.org/api/query?search_query=au:Freidel_L*&start=0&max_results=20");
}

As for why your alternative isn't working, it's because the usage is incorrect. Its should be:

System.Text.Encoding.UTF8.GetString()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文