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

本来在找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/";

 

posted on 2017-09-11 10:36  梦林``ysl  阅读(926)  评论(0编辑  收藏  举报

导航