码迷,mamicode.com
首页 > 其他好文 > 详细

C# IE代理操作

时间:2014-08-29 22:38:08      阅读:241      评论:0      收藏:0      [点我收藏+]

标签: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                 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     }

 

C# IE代理操作

标签:style   blog   http   color   os   io   ar   for   div   

原文地址:http://www.cnblogs.com/yomho/p/3945805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!