C#自动换ip功能或者ip代理功能要这么实现
2015-10-24 16:57
627 查看
源网址http://bbs.csdn.net/topics/390830518
class Program{[DllImport(@"wininet", SetLastError = true, CharSet = CharSet.Auto,EntryPoint = "InternetSetOption",CallingConvention = CallingConvention.StdCall)]public static extern bool InternetSetOption(int hInternet, int dmOption, IntPtr lpBuffer, int dwBufferLength);public static void SetProxy(string proxy){//打开注册表RegistryKey regKey = Registry.CurrentUser;string SubKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings"; RegistryKey optionKey = regKey.OpenSubKey(SubKeyPath, true); //更改健值,设置代理,optionKey.SetValue("ProxyEnable", 1);optionKey.SetValue("ProxyServer", proxy);//激活代理设置【用于即使IE没有关闭也能更新当前打开的IE中的代理设置。】InternetSetOption(0, 39, IntPtr.Zero, 0);InternetSetOption(0, 37, IntPtr.Zero, 0);}static void Main(string[] args){//本事例中未对代理服务器设置密码的情况进行尝试String ip = null;for (int i = 1; i < 254; i++){ip = "172.0.0." + i + ":808";//ip请替换为你需要查找的ip段SetProxy(ip);if (prcessBaidu()){Console.WriteLine(ip + "_____________TestOK"); break;}else{Console.WriteLine(ip + "__false");}}}//成功返回true,错误返回falsepublic static Boolean prcessBaidu(){HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.163.com"); myRequest.Method = "POST"; //采用post方式提交访问163主页 // Get responsetry//当无法访问163网站时,下面的对象会有错误产生,所以用try..catch处理掉这些异常{Stream newStream = myRequest.GetRequestStream();//获取请求流 // Send the data.newStream.Close();//关闭请求流HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();//获取应答对象StreamReader reader = new StreamReader(myResponse.GetResponseStream());//获取应答流string content = reader.ReadToEnd();//将流对象读取到string 中if (content.IndexOf("http://reg.163.com") > -1)//如果访问网站成功,则网页中包含置顶的关键字符串“http://reg.163.com”表示访问网页成功{return true;}else{return false;}}catch (Exception ex){}return false;}}
相关文章推荐
- C#几个基本概念
- 关于C#接口作用的理解
- C# 连接自动拨号与断开分析
- C#利用System.Uri转URL为绝对地址的方法
- c#换ip代理源码
- C# 验证代理IP的方法
- C#加密算法汇总(推荐)
- C# 编写简单的控制台应用程序 显示如下数字金字塔
- C#接口作用的深入理解
- C#入门经典
- C#Monitor类 Lock Mutex类详解
- C# LnkHelper
- c#后台调用脚本
- C# 因IIS回收导致定时器失效的解决方案
- C# 简单通信(一对多) 新增文件传输进度
- 20151024_002_C#基础知识(ArrayList,Hashtable,List,Dictionary)
- fields must be fully assigned before control is returned to the caller解决方案
- C#中Trim()、TrimStart()、TrimEnd()的用法
- 20151024_001_C#基础知识(静态与非静态的区别,值类型和引用类型,堆和栈的区别,字符串的不可变性,命名空间)
- C#winform窗体用户控件自定义事件