浏览器运行时的代理连接

时间:2012-12-26 11:29:03

标签: visual-studio proxy registry

即使在浏览器运行时,我也必须创建连接/断开连接到代理服务器的应用程序。我发现我可以更改一些注册表项值。 这是我在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需要重新启动或打开代理设置才能重新加载代理信息。 如何强制浏览器重新加载代理配置?

修改 示例:ChrisProxy

2 个答案:

答案 0 :(得分:1)

您可以使用WebClient连接和设置代理。一个非常简单的例子如下所示。

Sub GetWebPageWithProxy(ByVal pathToUrl As String, ByVal pathToSaveFile As String)
    Dim wc As WebClient
    wc = New WebClient
    wc.Proxy = New WebProxy(New Uri("http://10.8.0.1:808"))
    wc.DownloadFile(pathToUrl, pathToSaveFile)
End Sub

答案 1 :(得分:1)

您应该在您的计划中致电:

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.

使用该代码,无需重新启动或打开代理设置即可重新加载代理信息。 INTERNET_OPTION_SETTINGS_CHANGED和INTERNET_OPTION_REFRESH会通知现有的Chrome和Internet Explorer实例。


备注:

  1. 您的通话程序无法成为服务。 InternetSetOption()是一个WinINet调用。
  2. 它应该在同一用户(浏览器)帐户下运行。