c# 设置代理IP 访问网页

WebProxy proxyObject = new WebProxy(_ip, _port);//str为IP地址 port为端口号 代理类
HttpWebRequest Req = (HttpWebRequest)WebRequest.Create("http://www.yusige.com/"); // 访问这个网站 ,返回的就是你发出请求的代理ip 这个做代理ip测试非常方便,可以知道代理是否成功
Req.UserAgent = Wport.Com.AgentIps.GetUserAgent();

Req.Proxy = proxyObject; //设置代理
Req.Method = "GET";
Req.KeepAlive = true;
//Req.Timeout = 5000;
HttpWebResponse Resp = (HttpWebResponse)Req.GetResponse();
Encoding code = Encoding.GetEncoding("UTF-8");
using (StreamReader sr = new StreamReader(Resp.GetResponseStream(), code))
{
sr.ReadToEnd();//获取得到的网址html返回数据,这里就可以使用某些解析html的dll直接使用了,比如htmlpaser
}

猜你喜欢

转载自www.cnblogs.com/edukong/p/11772157.html