我必须制作即使在浏览器运行时也可以连接/断开与代理服务器的连接的应用程序。我发现可以更改某些注册表项值。
这是我在Visual Basic中的代码:
Imports Microsoft.Win32
Public Class Form1
Public Sub SetProxy()
On Error Resume Next
Dim regkey1 As RegistryKey
regkey1 = Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", RegistryKeyPermissionCheck.Default)
regkey1.SetValue("ProxyServer", "ftp=10.8.0.1:808;http=10.8.0.1:808;https=10.8.0.1:808;socks=10.8.0.1:1080", RegistryValueKind.Unknown)
regkey1.SetValue("ProxyEnable", True, RegistryValueKind.DWord)
regkey1.Close()
Label1.Text = "Connected to Disa's Proxy Server"
Label1.ForeColor = Color.Green
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
On Error Resume Next
SetProxy()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
On Error Resume Next
Dim regKey As RegistryKey
regKey = Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", RegistryKeyPermissionCheck.Default)
regKey.SetValue("ProxyEnable", False, RegistryValueKind.DWord)
regKey.Close()
Label1.Text = "Disconnected from Disa's Proxy Server"
Label1.ForeColor = Color.Red
End Sub
End Class
此代码在Firefox上运行良好,但在IE和Chrome上却无法运行。
IE打开后,它会阻止
Internet Settings
中的所有注册表更改。 Chrome需要重新启动或打开Proxy设置才能重新加载代理信息。如何强制浏览器重新加载代理配置?
编辑
示例:ChrisProxy
最佳答案
您应该在程序中调用:
InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
// Notifies the system that the registry settings have been changed
// so that it verifies the settings on the next call to InternetConnect.
InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0);
// Causes the proxy data to be reread from the registry for a handle.
// No buffer is required.
使用该代码,无需重新启动或打开代理设置即可重新加载代理信息。现有的Chrome和Internet Explorer实例由INTERNET_OPTION_SETTINGS_CHANGED和INTERNET_OPTION_REFRESH通知。
笔记:
您的调用程序不能作为服务。 InternetSetOption()是WinINet调用。
它应在同一用户(与浏览器相同)帐户下运行。