在很多时候我们需要用到IP代理,比如爬虫、投票等
封IP是一种很常用的办法,所谓道高一尺、魔高一丈,IP代理应运而生
最简单的一段代码
static void Main(string[] args) { var httpRequest = (HttpWebRequest)WebRequest.Create("http://1212.ip138.com/ic.asp"); httpRequest.Method = "GET"; httpRequest.Credentials = CredentialCache.DefaultCredentials; // 设置代理属性WebProxy ------------------------------------------------- var proxy = new WebProxy {Address = new Uri("http://59.62.112.203:808/")}; //proxy.Credentials = new NetworkCredential("xxx", "xxx"); // 在发起HTTP请求前将proxy赋值给HttpWebRequest的Proxy 属性 httpRequest.Proxy = proxy; //------------------------------------------------- var res = (HttpWebResponse)httpRequest.GetResponse(); var reader = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("GB2312")); var content = reader.ReadToEnd(); reader.Close(); Console.WriteLine(content); Console.Read(); }
这样看来,其实使用代理也很简单,网络上有很多公开的代理IP地址,比如在这里:http://www.xicidaili.com/