C# version
/// <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 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);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…