在 C# 中以编程方式设置浏览器代理设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/197725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically Set Browser Proxy Settings in C#
提问by John Miller
I'm writing an winforms app that needs to set internet explorer's proxy settings and then open a new browser window. At the moment, I'm applying the proxy settings by going into the registry:
我正在编写一个需要设置 Internet Explorer 代理设置的 winforms 应用程序,然后打开一个新的浏览器窗口。目前,我正在通过进入注册表来应用代理设置:
RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", "127.0.0.1:8080");
Is going into the registry the bestway to do this, or is there a more recommended approach? I'd like to avoid registry changes if there's an alternative solution.
进入注册表是执行此操作的最佳方法,还是有更推荐的方法?如果有替代解决方案,我想避免更改注册表。
采纳答案by JSB????
This depends somewhat on your exact needs. If you are writing a C# app and simply want to set the default proxy settings that your app will use, use the class System.Net.GlobalProxySelection (http://msdn.microsoft.com/en-us/library/system.net.globalproxyselection.aspx). You can also set the proxy for any particular connection with System.Net.WebProxy (http://msdn.microsoft.com/en-us/library/system.net.webproxy.aspx).
这在某种程度上取决于您的确切需求。如果您正在编写 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) 的任何特定连接设置代理。
If you actually want to update the proxy settings in the registry, I believe that you'll need to use P/Invoke to call the WinAPI function WinHttpSetDefaultProxyConfiguration (http://msdn.microsoft.com/en-us/library/aa384113.aspx).
如果你真的想更新注册表中的代理设置,我相信你需要使用 P/Invoke 来调用 WinAPI 函数 WinHttpSetDefaultProxyConfiguration ( http://msdn.microsoft.com/en-us/library/aa384113. aspx)。
回答by JaredPar
Check out this KB article specifically tagged at what you're trying to do.
查看这篇 KB 文章,专门标记了您正在尝试执行的操作。
http://support.microsoft.com/kb/226473
http://support.microsoft.com/kb/226473
The short version is you want to use the InternetOpen, InternetSetOption API's to update the proxy settings.
简短版本是您要使用 InternetOpen、InternetSetOption API 来更新代理设置。
回答by chris
from: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/19517edf-8348-438a-a3da-5fbe7a46b61a
来自:http: //social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/19517edf-8348-438a-a3da-5fbe7a46b61a
Add these lines at the beginning of your code:
在代码的开头添加这些行:
using System.Runtime.InteropServices; using Microsoft.Win32;
使用 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;
And imply the code:
并暗示代码:
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);
回答by Dave
Quick Code example (from msdn):
快速代码示例(来自 msdn):
WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
WebRequest req = WebRequest.Create("http://www.contoso.com");
req.Proxy = proxyObject;
回答by Naim Raja Díaz
You can use this useful method existing since FW 2.0: (i've just discovered and i'm another man now...)
你可以使用这个自 FW 2.0 以来就存在的有用方法:(我刚刚发现,现在我是另一个人......)
http://msdn.microsoft.com/en-us/library/system.net.webrequest.getsystemwebproxy.aspx
http://msdn.microsoft.com/en-us/library/system.net.webrequest.getsystemwebproxy.aspx
回答by 131
I wrote a 10 lines program to do that, feel free to try https://github.com/131/proxytoggle
我写了一个 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);
}
}
}