3

ブラウザ起動中でもプロキシサーバーに接続/切断するアプリケーションを作らなければなりません。一部のレジストリ キーの値を変更できることがわかりました。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

4

2 に答える 2

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.

そのコードを使用すると、プロキシ情報をリロードするためにプロキシ設定を再起動したり開いたりする必要はありません。既存の Chrome および Internet Explorer インスタンスは、INTERNET_OPTION_SETTINGS_CHANGED および INTERNET_OPTION_REFRESH によって通知されます。


ノート:

  1. 呼び出しプログラムをサービスにすることはできません。InternetSetOption() は WinINet 呼び出しです。
  2. 同じユーザー (ブラウザーの時点) のアカウントで実行する必要があります。
于 2014-11-26T14:57:21.093 に答える
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
于 2012-12-26T16:51:14.643 に答える