WebClient.DownloadString 由于编码问题导致字符损坏,但浏览器没问题 [英] WebClient.DownloadString results in mangled characters due to encoding issues, but the browser is OK
问题描述
以下代码:
var text = (new WebClient()).DownloadString("http://export.arxiv.org/api/query?search_query=au:Freidel_L*&start=0&max_results=20"));
产生一个变量 text
,其中包含字符串
results in a variable text
that contains, among many other things, the string
$κ$-Minkowski 空间、标量场和洛伦兹不变性问题"
"$κ$-Minkowski space, scalar field, and the issue of Lorentz invariance"
但是,当我在 Firefox 中访问该 URL 时,我得到
However, when I visit that URL in Firefox, I get
$κ$-Minkowski 空间、标量场和洛伦兹不变性问题
$κ$-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.
我不确定这里的错误在哪里.提要是否在说谎是 UTF8 编码,浏览器是否足够聪明,可以弄清楚这一点,但不是 WebClient
?提要是否正确 UTF8 编码,但 WebClient
以其他方式失败?我可以做些什么来缓解这种情况?
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?
推荐答案
这不是说谎.您应该在调用 DownloadString 之前先设置 webclient 的编码.
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()
这篇关于WebClient.DownloadString 由于编码问题导致字符损坏,但浏览器没问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!