WebClient.DownloadString 由于编码问题导致字符损坏,但浏览器正常
以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是说谎。在调用 DownloadString 之前,您应该先设置 Web 客户端的编码。
至于为什么你的替代方案不起作用,那是因为用法不正确。它应该是:
It's not lying. You should set the webclient's encoding first before calling DownloadString.
As for why your alternative isn't working, it's because the usage is incorrect. Its should be: