如何在 HtmlAgilityPack 中使用代理

问题描述 投票:0回答:1

我需要使用带有 HtmlAgilityPack 的代理。 我提供了我的应用程序的链接

RefURL
。之后我希望应用程序从代理地址获取 url。例如“101.109.44.157:8080”

我搜索并发现了这个:

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

并像这样使用它。

RefURL = new Uri(refLink.Text);

WebClient wc = new WebClient();
wc.Proxy = new WebProxy("101.109.44.157:8080");
var page = wc.DownloadString(RefURL);

RefURL.ToString();
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = web.Load(RefURL.ToString());

但是不起作用!

c# html-agility-pack http-proxy
1个回答
8
投票

需要在此代码行中传递网络代理:

HtmlAgilityPack.HtmlDocument doc = web.Load(RefURL.ToString(),"GET", webProxy);

第一步是找到“新鲜代理IP”列表,例如:

这些地址中的大多数都可以工作几个小时。查看如何在浏览器中设置代理IP。如果代理是匿名的,此页面(IP查找)应该无法检测到您的实际位置和IP。

一旦您拥有可用的代理 IP 和端口,您就可以创建

webProxy
对象或简单地传递 IP 和端口。

string RefURL = "https://www.whatismyip.com/";
string myProxyIP = "119.81.197.124"; //check this is still available
int myPort = 3128;
string userId = string.Empty; //leave it blank
string password = string.Empty;
try
{
    HtmlWeb web = new HtmlWeb();
    var doc = web.Load(RefURL.ToString(), myProxyIP, myPort, userId, password);
    Console.WriteLine(doc.DocumentNode.InnerHtml);
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
© www.soinside.com 2019 - 2024. All rights reserved.