zoukankan      html  css  js  c++  java
  • C# IE代理操作

     1 public class IPProxy
     2     {
     3         [System.Runtime.InteropServices.DllImport("wininet.dll", SetLastError = true)]
     4         private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lPBuffer, int lpdwBufferLength);
     5         private const int INTERNET_OPTION_REFRESH = 0x000025;
     6         private const int INTERNET_OPTION_SETTINGS_CHANGED = 0x000027;
     7 
     8         public static bool Proxy(string ip,int port) {
     9             Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SoftwareMicrosoftWindowsCurrentVersionInternet Settings", true);
    10             //设置代理可用 
    11             rk.SetValue("ProxyEnable", 1);
    12             //设置代理IP和端口 
    13             rk.SetValue("ProxyServer", string.Format("{0}:{1}", ip, port));
    14             rk.Close();
    15             //使它立即生效
    16             InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
    17             InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
    18             if (IsUseful("http://www.baidu.com/")) {
    19                 return true;
    20             }
    21             CancelProxy();
    22             return false;
    23         }
    24         public static void CancelProxy() {
    25             Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SoftwareMicrosoftWindowsCurrentVersionInternet Settings", true);
    26             rk.SetValue("ProxyEnable", 0);
    27         }
    28         public static bool IsUseful(string ipOrHost) {
    29             using (System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping())
    30             {
    31                 
    32                 try
    33                 {
    34 
    35                     return System.Net.NetworkInformation.IPStatus.Success.Equals(pingSender.Send(ipOrHost,6000).Status);
    36                 }
    37                 catch {
    38                     return false;
    39                 }
    40             }
    41 
    42         }
    43     }
  • 相关阅读:
    js Math对象
    extjs 获取Dom对象
    easyui validatebox 验证集合
    Ext.Ajax.request与form.submit的用法区别
    js获取url参数值
    【原创】extjs4做的grid,带分页,搜索
    SqlServer2005数据库同步
    【原创】jquery实现动态多组图片切换
    easyui表单数据验证
    对象模型图【OMD】阅读指南
  • 原文地址:https://www.cnblogs.com/yomho/p/3945805.html
Copyright © 2011-2022 走看看