【问题标题】:Programmatically Set Browser Proxy Settings in C#在 C# 中以编程方式设置浏览器代理设置
【发布时间】:2010-09-16 21:33:25
【问题描述】:

我正在编写一个 winforms 应用程序,它需要设置 Internet Explorer 的代理设置,然后打开一个新的浏览器窗口。目前,我正在通过进入注册表来应用代理设置:

RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", "127.0.0.1:8080");

进入注册表是做到这一点的最佳方法,还是有更推荐的方法?如果有其他解决方案,我想避免更改注册表。

【问题讨论】:

    标签: c# proxy registry


    【解决方案1】:

    我为此编写了一个 10 行的程序,请随意尝试https://github.com/131/proxytoggle

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    using Microsoft.Win32;
    
    namespace ProxyToggle
    {
    
        class Program
        {
    
            [DllImport("wininet.dll")]
            public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
            public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
            public const int INTERNET_OPTION_REFRESH = 37;
    
    
            static void setProxy(string proxyhost, bool proxyEnabled)
            {
                const string userRoot = "HKEY_CURRENT_USER";
                const string subkey = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
                const string keyName = userRoot + "\\" + subkey;
    
                Registry.SetValue(keyName, "ProxyServer", proxyhost);
                Registry.SetValue(keyName, "ProxyEnable", proxyEnabled?"1": "0");
    
                // These lines implement the Interface in the beginning of program 
                // They cause the OS to refresh the settings, causing IP to realy update
                InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
                InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
            }
    
            static void Main(string[] args)
            {
                if (args.Length == 0)
                {
                    setProxy("", false);
                    return;
                }
    
                setProxy(args[0], true);
            }
        }
    }
    

    【讨论】:

    • 您的代码不起作用,因为应该将数字 0 或 1 设置为 ProxyEnable。您正在设置一个字符串,但这不起作用。我正在修复它
    • 我的代理服务器正在由组策略或其他东西自动设置。当我通过执行应用程序以您的方式更改它时,它工作得很好。当我尝试从 Windows 服务注册表值设置但立即恢复为策略默认值时,当我进行任何这些 InternetSetOption 调用时。如果我不调用 InternetSetOption` - 注册表值会更改,但不会更改实际的活动代理。不知道为什么。
    • 是的,可以确认,Registry.SetValue(keyName, "ProxyEnable", proxyEnabled?"1": "0");不行,但是 Registry.SetValue(keyName, "ProxyEnable", proxyEnabled? 1: 0);工作正常。 (值不应作为字符串传递)
    【解决方案2】:

    您可以使用自 FW 2.0 以来存在的这种有用的方法: (我刚刚发现,我现在是另一个人了......)

    http://msdn.microsoft.com/en-us/library/system.net.webrequest.getsystemwebproxy.aspx

    【讨论】:

      【解决方案3】:

      快速代码示例(来自 msdn):

      WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
      WebRequest req = WebRequest.Create("http://www.contoso.com");
      req.Proxy = proxyObject;
      

      【讨论】:

      • 这不会改变 IE/WinINET 的代理设置。
      【解决方案4】:

      来自:http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/19517edf-8348-438a-a3da-5fbe7a46b61a

      在代码的开头添加这些行:

      使用 System.Runtime.InteropServices; 使用 Microsoft.Win32;

          [DllImport("wininet.dll")]
          public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
          public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
          public const int INTERNET_OPTION_REFRESH = 37;
          bool settingsReturn, refreshReturn;
      

      并暗示代码:

              RegKey.SetValue("ProxyServer", YOURPROXY);
              RegKey.SetValue("ProxyEnable", 1);
      
              // These lines implement the Interface in the beginning of program 
              // They cause the OS to refresh the settings, causing IP to realy update
              settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
              refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
      

      【讨论】:

        【解决方案5】:

        这在一定程度上取决于您的确切需求。如果您正在编写 C# 应用程序并且只想设置您的应用程序将使用的默认代理设置,请使用类 System.Net.GlobalProxySelection (http://msdn.microsoft.com/en-us/library/system.net.globalproxyselection.aspx)。您还可以使用 System.Net.WebProxy (http://msdn.microsoft.com/en-us/library/system.net.webproxy.aspx) 为任何特定连接设置代理。

        如果你真的想更新注册表中的代理设置,我相信你需要使用P/Invoke来调用WinAPI函数WinHttpSetDefaultProxyConfiguration (http://msdn.microsoft.com/en-us/library/aa384113.aspx)。

        【讨论】:

        • 1. >如果你真的想更新代理设置in the registry
        • 据我所知,WinHttpSetDefaultProxyConfiguration 设置的 WinHTTP 代理设置与 Internet Explorer 设置相同。投票赞成的 13 个人中是否有人真正尝试过
        【解决方案6】:

        查看这篇知识库文章,专门标记了您正在尝试做的事情。

        http://support.microsoft.com/kb/226473

        简短的版本是您想使用 InternetOpen、InternetSetOption API 来更新代理设置。

        【讨论】:

        • 问题是如何在 C# 中做到这一点。知识库文章不在 C# 中。
        • @MarkGood 谁在乎?并非每个 API 都在 .NET 中重新实现。这就是 PInvoke 存在的原因。
        • @JaredPar 您共享的链接在 support.microsoft.com 上已不存在
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-19
        • 1970-01-01
        • 2013-04-20
        • 1970-01-01
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多