【问题标题】:Auto detect proxy settings if not set on system如果未在系统上设置,则自动检测代理设置
【发布时间】:2015-07-20 19:40:42
【问题描述】:
这里已经有几个问题涉及查找系统默认值并使用它们。这些有助于为我的应用程序提供“使用系统默认值”选项,但我偶尔会与甚至不知道他们的系统在代理后面的用户一起工作(并且可能有能力不足的 IT 人员),因此他们甚至可能不知道在他们的系统上有任何代理设置。
如何自动检测代理(如果可能)?我还没有找到可以提供足够帮助的帖子,而且 MDSN 也没有多大帮助...
【问题讨论】:
标签:
c#
proxy
network-programming
【解决方案1】:
如果我理解正确,您想检查代理设置是否启用,并获取它的设置。您可以使用以下代码(VB.Net),该代码可以轻松转换为启用/禁用代理并设置代理值的 C#。一般来说,你可以使用注册表来实现它:
Private Sub Enable_Proxy(ByVal Proxyset As String)
Dim regkeyProxy_Enable As RegistryKey
'Registry Key Location
regkeyProxy_Enable = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", True)
'Change The Proxy Enable value
regkeyProxy_Enable.SetValue("ProxyEnable", 1, RegistryValueKind.DWord)
'Set the proxy server value
regkeyProxy_Enable.SetValue("ProxyServer", Proxyset, RegistryValueKind.String)
End Sub
Private Sub Disable_Proxy()
Dim regkeyProxy_Enable As RegistryKey
regkeyProxy_Enable = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", True)
'Disable The proxy
regkeyProxy_Enable.SetValue("ProxyEnable", 0, RegistryValueKind.DWord)
End Sub
您可以使用此示例并根据需要对其进行修改。
请注意,以下键代表 Windows 机器上的代理设置,如果我没记错的话,它适用于 IE 和 Chrome,我记得 Firefox 使用自己的设置。
利隆