不幸的是,您不能为每个实例设置代理。甚至不是每个应用程序 - 它是全球性的。如果您将其更改为 .net 应用程序,它也会影响 IE。
这是更改代理的完整代码。
使用按钮“B_Get”、“B_Set”和“B_Disable”创建一个“Form1”,然后粘贴此代码。
它应该适合你。我将此应用程序用作我构建的自动代理循环器的概念证明。
根据您的应用程序需要,您可以切换它,然后再切换回来。请注意,它会影响 Internet Explorer。
要验证这是否有效,请检查 Internet Explorer。您可能需要打开/关闭 IE 的选项对话框,但您不需要重新启动浏览器。
这也会对 .NET 应用程序产生影响——如果您在运行程序时运行我的程序,您将能够更改程序正在使用的代理。
Imports Microsoft.Win32
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("wininet.dll")>
Public Shared Function InternetSetOption(hInternet As IntPtr, dwOption As Integer, lpBuffer As IntPtr, dwBufferLength As Integer) As Boolean
End Function
Public Const INTERNET_OPTION_SETTINGS_CHANGED As Integer = 39
Public Const INTERNET_OPTION_REFRESH As Integer = 37
'This function is what is called after editing the registry - this causes internet explorer to update its proxy even if it is already open.
'It also effects the web browser control in any VB.net application that is running.
Public Sub globalProxy_apply()
Dim settingsReturn As Boolean = False
Dim refreshReturn As Boolean = False
settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0)
If Not settingsReturn Then
MessageBox.Show("Error 001: Line ""InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0)"" failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0)
If Not refreshReturn Then
MessageBox.Show("Error 002: Line ""InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0)"" failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Public Function globalProxy_IsProxyEnabled() As Boolean
Try
Dim Regs As RegistryKey = Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", RegistryKeyPermissionCheck.ReadWriteSubTree)
If Regs.GetValue("ProxyEnable") <> Nothing Then
If Regs.GetValue("ProxyEnable").ToString() = "0" Then
Return False
Else
Return True
End If
Else
Return False
End If
Catch ex As Exception
MessageBox.Show("Error 01X: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
Public Function globalProxy_GetProxyServer() As String
Try
Dim Regs As RegistryKey = Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", RegistryKeyPermissionCheck.ReadWriteSubTree)
If Regs.GetValue("ProxyServer") <> Nothing Then
Return Regs.GetValue("ProxyServer").ToString()
Else
Return ""
End If
Catch ex As Exception
MessageBox.Show("Error 02X: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return ""
End Try
End Function
Public Sub globalProxy_DisableProxy()
Dim regkey As RegistryKey
Try
regkey = Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings")
regkey.SetValue("ProxyEnable", False, RegistryValueKind.DWord)
regkey.Close()
Catch ex As Exception
MessageBox.Show("Error 03X: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
globalProxy_apply()
End Sub
Public Sub globalProxy_SetProxy(ByVal ServerName As String)
Dim regkey As RegistryKey
Try
regkey = Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings")
regkey.SetValue("ProxyServer", ServerName, RegistryValueKind.Unknown)
regkey.SetValue("ProxyEnable", True, RegistryValueKind.DWord)
regkey.Close()
Catch ex As Exception
MessageBox.Show("Error 04X: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
globalProxy_apply()
End Sub
Private Sub B_Set_Click(sender As System.Object, e As System.EventArgs) Handles B_Set.Click
If TextBox1.Text = "" Then
globalProxy_DisableProxy()
Else
globalProxy_SetProxy(TextBox1.Text)
End If
End Sub
Private Sub B_Disable_Click(sender As System.Object, e As System.EventArgs) Handles B_Disable.Click
globalProxy_DisableProxy()
End Sub
Private Sub B_Get_Click(sender As System.Object, e As System.EventArgs) Handles B_Get.Click
If globalProxy_IsProxyEnabled() Then
TextBox1.Text = globalProxy_GetProxyServer()
Else
TextBox1.Text = ""
End If
End Sub
End Class