我已经使用.net 3.5,其上有一个WebBrowser控件有一个窗口的形式在Visual Studio 2008。 我需要分析窗体的POSTDATA在航行事件处理程序发送请求之前。 有没有办法得到它?
旧的win32浏览器控件有一个Before_Navigate事件,这不得不POSTDATA作为它的一个参数。 并非如此,新的.NET WebBrowser控件。
我已经使用.net 3.5,其上有一个WebBrowser控件有一个窗口的形式在Visual Studio 2008。 我需要分析窗体的POSTDATA在航行事件处理程序发送请求之前。 有没有办法得到它?
旧的win32浏览器控件有一个Before_Navigate事件,这不得不POSTDATA作为它的一个参数。 并非如此,新的.NET WebBrowser控件。
该功能不是由.NET WebBrowser控件暴露。 幸运的是,控制主要是围绕“老”控制的包装。 这使用类似以下(添加引用后SHDOCVW到您的项目),意味着你可以订阅你知道BeforeNavigate2事件和爱情(?):
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一个自己的浏览器组件,在未来的任何一点突破你的应用程序,例如。
所以,你不想把这段代码在任何你船的人很多和/或任何应保持对于很多框架版本来工作...
/// <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
});
}
保留以上,但更换:
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);