不幸的是,你cannnot設置每個insnance代理。甚至不是每個應用程序 - 它是全球性的。如果你使用.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
嗨,感謝那些鏈接。 但是,第一個鏈接指定在單個進程中設置webbrowser的代理。這意味着WebBrowser1,WebBrowser2都將具有相同的代理。不過,我希望每個人都有不同的代理。 第二個鏈接可以工作,但有一個主要問題。將文檔加載到瀏覽器後,它將使用默認代理下載我不想要的圖像,JavaScript文件等。 – 2013-02-28 06:52:44