我即将下载一个以 UTF-8 编码的页面。所以这是我的代码:
using (WebClient client = new WebClient())
{
client.Headers.Add("user-agent", Request.UserAgent);
htmlPage = client.DownloadString(HttpUtility.UrlDecode(resoruce_url));
var KeysParsed = HttpUtility.ParseQueryString(client.ResponseHeaders["Content-Type"].Replace(" ", "").Replace(";", "&"));
var charset = ((KeysParsed["charset"] != null) ? KeysParsed["charset"] : "UTF-8");
Response.Write(client.ResponseHeaders);
byte[] bytePage = Encoding.GetEncoding(charset).GetBytes(htmlPage);
using (var reader = new StreamReader(new MemoryStream(bytePage), Encoding.GetEncoding(charset)))
{
htmlPage = reader.ReadToEnd();
Response.Write(htmlPage);
}
}
所以,它UTF-8
为编码设置。但是下载的标题,例如,在我的屏幕上显示为:
Sexy cover: 60 e più di “quei dischi” vietati ai minori
而不是:
Sexy cover: 60 e più di “quei dischi” vietati ai minori
有些东西是错的,但我不知道在哪里。有任何想法吗?