技术开发 频道

c#通过代理访问网络


【IT168技术文档】

  方法1:修改注册表Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings下 ProxyEnable和ProxyServer。这种方法适用于局域网用户,拨号用户无效。
1 public partial class Form1 : Form 2 { 3 4 //用于刷新注册表 5 [DllImport(@"wininet", 6 SetLastError = true, 7 CharSet = CharSet.Auto, 8 EntryPoint = "InternetSetOption", 9 CallingConvention = CallingConvention.StdCall)] 10 11 12 public static extern bool InternetSetOption 13 ( 14 int hInternet, 15 int dmOption, 16 IntPtr lpBuffer, 17 int dwBufferLength 18 ); 19 20 private void btnStart_Click(object sender, EventArgs e) 21 { 22 RegistryKey pregkey; 23 pregkey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); 24 if (pregkey == null) 25 { 26 Console.WriteLine("键值不存在"); 27 } 28 else 29 { 30 pregkey.SetValue("ProxyEnable", 1); 31 pregkey.SetValue("ProxyServer", "代理地址"); 32 //激活代理设置 33 InternetSetOption(0, 39, IntPtr.Zero, 0); 34 InternetSetOption(0, 37, IntPtr.Zero, 0); 35 webBrowser1.Navigate(txtweb.Text, false); 36 // System.Threading.Thread.Sleep(10000); 37 } 38 } 39}
0
相关文章