C# 代理访问页面并获取数据

本文介绍了一种使用代理服务器访问Web的方法,包括通过C#代码设置HTTP代理以访问特定URL,并展示了如何利用Selenium WebDriver配合ChromeDriver实现浏览器级别的代理设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本来在找IWebDriver使用代理的方法,偶然获知这种的,记录并且共享

 

 1 WebProxy proxyObject = new WebProxy("125.31.19.26", 80);//str为IP地址 port为端口号 代理类
 2 HttpWebRequest Req = (HttpWebRequest)WebRequest.Create("http://ip.chemdrug.com/"); // 61.183.192.5
 5 Req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 133; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CIBA; InfoPath.2)";
 6 Req.Proxy = proxyObject; //设置代理 
 8 Req.Method = "GET";
 9 HttpWebResponse Resp = (HttpWebResponse)Req.GetResponse();
10 string StringSub = "";
11 string OkStr = "";
12 Encoding code = Encoding.GetEncoding("gb2312");
13 using (StreamReader sr = new StreamReader(Resp.GetResponseStream(), code))
14 {
16    string str1 = sr.ReadToEnd();//获取得到的网址html返回数据,这里就可以使用某些解析html的dll直接使用了,比如htmlpaser 
17 
18 }

 

 

selenium 

 1 ChromeOptions options = new ChromeOptions();
 2 Proxy proxy = new Proxy();
 3 proxy.Kind = ProxyKind.Manual;
 4 proxy.IsAutoDetect = false;
 5 proxy.HttpProxy =
 6 proxy.SslProxy = "125.31.19.26:80";
 7 options.Proxy = proxy;
 8 options.AddArgument("ignore-certificate-errors");
 9 var chromedriver = new ChromeDriver(options);
10  chromedriver.Url = "http://www.whatismyip.com.tw/";

 

转载于:https://www.cnblogs.com/yishilin/p/7503801.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值