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     }
  • 相关阅读:
    bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 [分块][LCT]
    luoguP1886 滑动窗口 [单调队列]
    bzoj1047: [HAOI2007]理想的正方形
    bzoj1012: [JSOI2008]最大数maxnumber [单调队列]
    树与二叉树之二--二叉树的性质与存储
    树与二叉树之一--基本概念与存储结构
    Markdown段首空格
    C++ atan2
    凸包学习笔记
    Codeforces Round #545 (Div. 1) E. Train Car Selection
  • 原文地址:https://www.cnblogs.com/yomho/p/3945805.html
Copyright © 2011-2022 走看看