我可以下载charset=iso-8859-1
的网页,并使用encoding=utf-8
下载吗?
它会正确下载吗?
我是否可以随时使用utf-8编码下载网络中的所有编码?
我的代码:
网页上的Html页面:
<html debug="true">
<head/>
<body>
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
<title>Untitled Document</title>
<meta name="robots" content="noindex"/>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
............
FUNC:
void download() {
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
worker.ReportProgress(i);
client.DownloadDataAsync(new Uri(link), i);
}
void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) {
Encoding enc = Encoding.UTF8;
string myString = enc.GetString(e.Result);
}
答案 0 :(得分:2)
不,这不起作用。 documentation of WebClient.Encoding
清楚地说:
使用DownloadString或DownloadStringAsync方法下载字符串时,WebClient使用此返回的Encoding将下载的Byte数组转换为字符串。
它为什么要起作用?您的网页编码与UTF-8不同。为什么想要在这里使用UTF-8?这没有道理。该文档编码为ISO 8859-1,因此这是您需要用来读取它的编码。