Visual studio 浏览器运行时的代理连接

Visual studio 浏览器运行时的代理连接,visual-studio,proxy,registry,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("So

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

编辑
示例:

您可以利用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

您应该在您的程序中呼叫:

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选项设置更改和Internet选项刷新得到通知


注意事项:

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.
  • 您的呼叫程序不能是服务。InternetSetOption()是一个WinINet调用
  • 它应该在同一个用户(与浏览器相同)帐户下运行

  • 我怀疑这不会有一个简单的答案,大概Chrome只是加载设置,这正是我应该做的。也许你可以修改你的代码,让你的代理可以一直使用,但通常什么都不做?我不想一直使用代理。F.e我想通过代理浏览一些页面并下载文件,而不需要。这就是为什么我希望能够像Firefox那样快速地改变它。问题是我通常不使用FF。我一直在考虑编写您自己的本地代理服务器,以便它可以使用默认设置,并根据需要切换到其他服务器。不管怎么说,看看你是如何寻找答案的,也许我遗漏了一些东西,但不要认为如果不这样做,不使用特定于浏览器的东西或网络堆栈上较低级别的东西,就很容易跨浏览器实现这一点。此IP是通过VP连接的我的服务器的IP。您可以在127.0.0.1(本地计算机)上使用代理不过,这在它们之间交换,就像在您自己设计的代理中一样。虽然这个问题很有趣,但我期待看到其他想法/解决方案。我可以编写“自己的浏览器”,使用我的代理来编写代码,对吗?我只想在Chrome和IEYes中强制重新加载代理设置。因此,您可以尝试真正实现什么,为什么需要打开浏览器?阅读这里的“动态变化”。(对于IE)不确定其他链接非常有用。谢谢