5

我在 Visual Studio 2008 中有一个使用 .NET 3.5 的 Windows 窗体,上面有一个 WebBrowser 控件。在发送请求之前,我需要在 Navigating 事件处理程序中分析表单的 PostData。有没有办法得到它?

旧的 win32 浏览器控件有一个 Before_Navigate 事件,该事件将 PostData 作为其参数之一。新的 .NET WebBrowser 控件并非如此。

4

2 回答 2

8

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;

和:

    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);
于 2012-08-17T08:22:38.057 回答
6

.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 组件。

因此,您不希望将此代码放在您交付给很多人的任何内容中和/或任何应该为未来的许多框架版本继续工作的内容中......

于 2008-09-27T20:03:47.737 回答