C# IE代理操作

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   ar   for   div   

 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(@"Software\Microsoft\Windows\CurrentVersion\Internet 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(@"Software\Microsoft\Windows\CurrentVersion\Internet 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                 try33                 {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     }

 

C# IE代理操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.