用C#编程设置浏览器代理设置

本文关键字:设置 浏览器 代理 编程 | 更新日期: 2023-09-27 17:48:49

我正在编写一个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#编程设置浏览器代理设置

这在一定程度上取决于您的确切需求。如果您正在编写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)。

来自: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);

我写了一个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;
            if(proxyhost.Length != 0)
               Registry.SetValue(keyName, "ProxyServer", proxyhost);
            Registry.SetValue(keyName, "ProxyEnable", proxyEnabled ? "1" : "0", RegistryValueKind.DWord);
            // 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);
        }
    }
}

查看这篇专门标记了你要做的事情的知识库文章。

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

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

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

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

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

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