给位路过的大神我想实现C#自动换ip功能或者ip代理功能要这么实现呢

时间:2021-11-20 13:39:26
我对这一块完全不懂在网上也找了很多资料都说的不清不楚的,也有可能是我没有看明白    
那位大神对这快懂的能不能跟我详细的说下   或者把已经实现的代码发给我让我学习下谢谢
大神们了

5 个解决方案

#2


百度的

using System; 
using System.Collections.Generic; using System.Linq; using System.Text; 
using System.Runtime.InteropServices; using System.Diagnostics; using Microsoft.Win32; 
using System.Net.NetworkInformation; using System.Net; using System.IO;    
namespace TestBlog { 
    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,错误返回false  
     public static Boolean prcessBaidu(){    
      HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.163.com");        myRequest.Method = "POST"; //采用post方式提交访问163主页 // Get response      
     try//当无法访问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;                        }     } }

#3


帮你整理了一下

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,错误返回false  
    public static Boolean prcessBaidu()
    {
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.163.com"); myRequest.Method = "POST"; //采用post方式提交访问163主页 // Get response      
        try//当无法访问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;
    }
}

#4


引用 3 楼 sunny906 的回复:
帮你整理了一下

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,错误返回false  
    public static Boolean prcessBaidu()
    {
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.163.com"); myRequest.Method = "POST"; //采用post方式提交访问163主页 // Get response      
        try//当无法访问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;
    }
}

大神  我用你的代码测试了下运行后网络连接失败了

#5


引用 3 楼 sunny906 的回复:
帮你整理了一下

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,错误返回false  
    public static Boolean prcessBaidu()
    {
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.163.com"); myRequest.Method = "POST"; //采用post方式提交访问163主页 // Get response      
        try//当无法访问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#自动换ip功能或者ip代理功能要这么实现呢给位路过的大神我想实现C#自动换ip功能或者ip代理功能要这么实现呢
都是这样子要这么处理呢

#1


#2


百度的

using System; 
using System.Collections.Generic; using System.Linq; using System.Text; 
using System.Runtime.InteropServices; using System.Diagnostics; using Microsoft.Win32; 
using System.Net.NetworkInformation; using System.Net; using System.IO;    
namespace TestBlog { 
    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,错误返回false  
     public static Boolean prcessBaidu(){    
      HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.163.com");        myRequest.Method = "POST"; //采用post方式提交访问163主页 // Get response      
     try//当无法访问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;                        }     } }

#3


帮你整理了一下

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,错误返回false  
    public static Boolean prcessBaidu()
    {
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.163.com"); myRequest.Method = "POST"; //采用post方式提交访问163主页 // Get response      
        try//当无法访问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;
    }
}

#4


引用 3 楼 sunny906 的回复:
帮你整理了一下

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,错误返回false  
    public static Boolean prcessBaidu()
    {
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.163.com"); myRequest.Method = "POST"; //采用post方式提交访问163主页 // Get response      
        try//当无法访问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;
    }
}

大神  我用你的代码测试了下运行后网络连接失败了

#5


引用 3 楼 sunny906 的回复:
帮你整理了一下

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,错误返回false  
    public static Boolean prcessBaidu()
    {
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.163.com"); myRequest.Method = "POST"; //采用post方式提交访问163主页 // Get response      
        try//当无法访问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#自动换ip功能或者ip代理功能要这么实现呢给位路过的大神我想实现C#自动换ip功能或者ip代理功能要这么实现呢
都是这样子要这么处理呢