Skip to main content
 首页 » 编程设计

c#之使用 WebClient 加载 HTML

2024年02月02日28wayfarer

我正在尝试下载网页:

string remoteUri = "http://whois.domaintools.com/94.100.179.159"; 
WebClient myWebClient = new WebClient(); 
byte[] myDataBuffer = myWebClient.DownloadData(remoteUri); 
string download = Encoding.ASCII.GetString(myDataBuffer); 
HtmlDocument doc = new HtmlDocument(); 
doc.LoadHtml(download); 
doc.Save("file1.htm"); 

出现错误

webexception was unhandled: (403) Forbidden.

还有其他方法可以下载页面吗? 我试过 HtmlDocument 类,但正如我所见,它需要在浏览器中加载网页。

HtmlWeb hwObject = new HtmlWeb(); 
        string ip = "http://whois.domaintools.com/"; 
        HtmlAgilityPack.HtmlDocument htmldocObject = hwObject.Load(ip); 
 
        foreach (HtmlNode link in htmldocObject.DocumentNode.SelectNodes("//meta[@name = 'description']")) 
        { 
            ... 
        } 

请您参考如下方法:

using (var myWebClient = new WebClient()) 
{ 
    myWebClient.Headers["User-Agent"] = "MOZILLA/5.0 (WINDOWS NT 6.1; WOW64) APPLEWEBKIT/537.1 (KHTML, LIKE GECKO) CHROME/21.0.1180.75 SAFARI/537.1"; 
 
    string page = myWebClient.DownloadString("http://whois.domaintools.com/94.100.179.159"); 
 
    HtmlDocument doc = new HtmlDocument(); 
    doc.LoadHtml(page); 
}