WebBrowser.NavigatingイベントハンドラーからPostDataにアクセスするにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/141626

  •  02-07-2019
  •  | 
  •  

質問

Visual Studio 2008には、WebBrowserコントロールを持つ.NET 3.5を使用したWindowsフォームがあります。リクエストを送信する前に、NavigatingイベントハンドラーでフォームのPostDataを分析する必要があります。それに到達する方法はありますか?

古いwin32ブラウザーコントロールには、引数の1つとしてPostDataを持つBefore_Navigateイベントがありました。新しい.NET WebBrowserコントロールではそうではありません。

役に立ちましたか?

解決

その機能は、.NET WebBrowserコントロールによって公開されません。幸いなことに、そのコントロールはほとんど「古い」コントロールのラッパーです。これは、次のようなものを使用して、知っているBeforeNavigate2イベントにサブスクライブできることを意味します(SHDocVwへの参照をプロジェクトに追加した後):

Dim ie = DirectCast(WebBrowser1.ActiveXInstance, SHDocVw.InternetExplorer)
AddHandler ie.BeforeNavigate2, AddressOf WebBrowser_BeforeNavigate2

...そして、そのイベント内のPostDataに対して必要な処理を実行します。

Private Sub WebBrowser_BeforeNavigate2(ByVal pDisp As Object, ByRef URL As Object, _
       ByRef Flags As Object, ByRef TargetFrameName As Object, _
       ByRef PostData As Object, ByRef Headers As Object, ByRef Cancel As Boolean)
    Dim PostDataText = System.Text.Encoding.ASCII.GetString(PostData)
End Sub

重要な注意事項:ドキュメントWebBrowser.ActiveXInstanceプロパティは、「このAPIは.NET Frameworkインフラストラクチャをサポートするものであり、コードから直接使用することを意図したものではない」と述べています。言い換えると、プロパティを使用すると、たとえばフレームワークの人々が既存のSHDocVw COMコンポーネントをラップする代わりに独自のブラウザコンポーネントを実装することにした場合など、将来の任意の時点でアプリが破損する可能性があります。

だから、多くの人に出荷するものや、今後多くのフレームワークバージョンで機能し続けるものにこのコードを入れたくないでしょう...

他のヒント

C#バージョン

    /// <summary>
    /// Fires before navigation occurs in the given object (on either a window or frameset element).
    /// </summary>
    /// <param name="pDisp">Object that evaluates to the top level or frame WebBrowser object corresponding to the navigation.</param>
    /// <param name="url">String expression that evaluates to the URL to which the browser is navigating.</param>
    /// <param name="Flags">Reserved. Set to zero.</param>
    /// <param name="TargetFrameName">String expression that evaluates to the name of the frame in which the resource will be displayed, or Null if no named frame is targeted for the resource.</param>
    /// <param name="PostData">Data to send to the server if the HTTP POST transaction is being used.</param>
    /// <param name="Headers">Value that specifies the additional HTTP headers to send to the server (HTTP URLs only). The headers can specify such things as the action required of the server, the type of data being passed to the server, or a status code.</param>
    /// <param name="Cancel">Boolean value that the container can set to True to cancel the navigation operation, or to False to allow it to proceed.</param>
    private delegate void BeforeNavigate2(object pDisp, ref dynamic url, ref dynamic Flags, ref dynamic TargetFrameName, ref dynamic PostData, ref dynamic Headers, ref bool Cancel);

    private void Form1_Load(object sender, EventArgs e)
    {
        dynamic d = webBrowser1.ActiveXInstance;

        d.BeforeNavigate2 += new BeforeNavigate2((object pDisp,
            ref dynamic url,
            ref dynamic Flags,
            ref dynamic TargetFrameName,
            ref dynamic PostData,
            ref dynamic Headers,
            ref bool Cancel) => {

            // Do something with PostData
        });
    }


C#WPFバージョン

上記を保持しますが、置き換えます:

    dynamic d = webBrowser1.ActiveXInstance;

with:

    using System.Reflection;
    ...
    PropertyInfo prop = typeof(System.Windows.Controls.WebBrowser).GetProperty("ActiveXInstance", BindingFlags.NonPublic | BindingFlags.Instance);
    MethodInfo getter = prop.GetGetMethod(true);
    dynamic d = getter.Invoke(webBrowser1, null);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top