using a proxy with htmlagilitypack

喜欢而已 提交于 2019-11-28 10:38:33

Use an overload of HtmlWeb.Load() that uses proxies. There are two overload signatures:

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

I don't have any first-hand experience using proxies in my code but I'd expect this to work.

HtmlAgilityPack doesn't download data from url. Use a class to download the page that supports Proxy.

For example

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

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);

EDIT

Assuming you read something like 11.22.33.44:5678 from text file, it is also possible to create the proxy as

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

Within our corporate setup, adding this to app.config works for me without the need for any code changes

<system.net>
   <defaultProxy useDefaultCredentials="true" />
</system.net>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!