5
Uri url = new Uri("http://localhost/rgm.php");
WebClient client = new WebClient();
string html = client.DownloadString(url);

HtmlAgilityPack.HtmlDocument doc23 = new HtmlAgilityPack.HtmlDocument();
doc23.LoadHtml(html);

HtmlNode body23 = doc23.DocumentNode.SelectSingleNode("//body");

string content23 = body23.InnerHtml;

我如何强制它使用“UTF-8”编码解析网页?

4

3 回答 3

5

使用DownloadDataWebClient 的方法代替DownloadString()

WebClient client = new WebClient();
var data = client.DownloadData(url);
var html = Encoding.UTF8.GetString(data);
于 2013-08-16T09:29:54.323 回答
3

使用内存流

WebClient client = new WebClient(); 
MemoryStream ms = new MemoryStream(client.DownloadData("http://localhost/rgm.php"));

HtmlDocument doc23 = new HtmlDocument();
doc23.Load(ms, Encoding.UTF8);

HtmlNode body23 = doc23.DocumentNode.SelectSingleNode("//body");
string content23 = body23.InnerHtml;
于 2016-03-21T04:20:11.433 回答
0

它可能是一个替代方案。

string url = "http://localhost/rgm.php";
            var Webget = new HtmlWeb();

 Webget.OverrideEncoding = Encoding.UTF8;
            var doc23 = Webget.Load(url);

HtmlNode body23 = doc23.DocumentNode.SelectSingleNode("//body");
string content23 = body23.InnerHtml;
于 2016-06-14T13:54:11.360 回答