How can I access PostData from WebBrowser.Navigating event handler?

前端 未结 2 1408
无人共我
无人共我 2020-12-09 14:24

I\'ve got a windows form in Visual Studio 2008 using .NET 3.5 which has a WebBrowser control on it. I need to analyse the form\'s PostData in the Navigating event handler b

2条回答
  •  借酒劲吻你
    2020-12-09 14:29

    C# version

        /// 
        /// Fires before navigation occurs in the given object (on either a window or frameset element).
        /// 
        /// Object that evaluates to the top level or frame WebBrowser object corresponding to the navigation.
        /// String expression that evaluates to the URL to which the browser is navigating.
        /// Reserved. Set to zero.
        /// 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.
        /// Data to send to the server if the HTTP POST transaction is being used.
        /// 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.
        /// Boolean value that the container can set to True to cancel the navigation operation, or to False to allow it to proceed.
        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 version

    Keep the above, but replace:

        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);
    

提交回复
热议问题