2013-08-16 72 views
3
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」編碼的網頁? Web客戶端的C#HtmlAgilityPack HtmlDocument()LoadHtml編碼

回答

3

使用DownloadData法代替DownloadString()

WebClient client = new WebClient(); 
var data = client.DownloadData(url); 
var html = Encoding.UTF8.GetString(data); 
3

使用的MemoryStream

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; 
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;