以UTF-8下载网页 [英] Download a webpage in UTF-8
本文介绍了以UTF-8下载网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I'm using the code below to download this XML file:
private async static Task<string> DownloadPageAsync(string url)
{
try
{
HttpClientHandler handler = new HttpClientHandler();
handler.UseDefaultCredentials = true;
handler.AllowAutoRedirect = true;
handler.UseCookies = true;
HttpClient client = new HttpClient(handler);
client.MaxResponseContentBufferSize = 10000000;
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody = response.Content.ReadAsString();
return responseBody;
}
catch (Exception ex)
{
return "error" + ex.Message;
}
}
但是我得到的文档似乎存在编码问题.尽管文档格式不正确,但我猜我下载的网页也不是UTF-8.如何返回UTF-8字符串?谢谢.
but the document I'm getting seems to have encoding problems. Although the document is not well formatted, I'm guessing my downloaded webpage is not in UTF-8 either. How can I return a UTF-8 string? Thanks.
推荐答案
您的链接编码为 iso-8859-1 .
使用
XmlDocument.Load (uriString)
XmlDocument.Load(uriString)
或
XDocument.Load (uriString)
XDocument.Load(uriString)
这篇关于以UTF-8下载网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文