使用htmlagilitypack代理

我搜索了这个问题,但没有找到我正在寻找的任何内容,基本上我想使用htmlagilitypack的代理,我之前有代码执行但丢失了,这是我到目前为止的代码,这是工作的。 但我在我正在制作的程序上计时并且需要启用代理。

private void button1_Click(object sender, EventArgs e) { StringBuilder output = new StringBuilder(); string raw = "http://www.google.com"; HtmlWeb webGet = new HtmlWeb(); webGet.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6"; var document = webGet.Load(raw); } 

使用使用代理的HtmlWeb.Load()重载。 有两个过载签名:

 HtmlDocument Load(string url, string method, WebProxy proxy, NetworkCredential credentials); HtmlDocument Load(string url, string proxyHost, int proxyPort, string userId, string password); 

我没有在我的代码中使用代理的任何第一手经验,但我希望这可以工作。

HtmlAgilityPack不会从url下载数据。 使用类下载支持Proxy的页面。

例如

 WebClient wc = new WebClient(); wc.Proxy = new WebProxy(host,port); var page = wc.DownloadString(url); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(page); 

编辑

假设您从文本文件中读取了类似11.22.33.44:5678的内容,也可以将代理创建为

 wc.Proxy = new WebProxy("11.22.33.44:5678"); 

在我们的公司设置中,将此添加到app.config对我而言无需任何代码更改