如何从WebBrowser.Navigating事件处理程序访问的PostData?事件、程序、WebBrowser、Navigating

2023-09-02 02:01:09 作者:2.我就再吃一口

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

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 before the request is sent. Is there a way to get to it?

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

The old win32 browser control had a Before_Navigate event which had PostData as one of its arguments. Not so with the new .NET WebBrowser control.

推荐答案

这功能没有被.NET WebBrowser控件暴露。幸运的是,控制主要是围绕着'老'控制的包装。使用像这意味着您可以订阅BeforeNavigate2事件,你知道,爱以下(增加提及SHDOCVW到项目后)(?):

That functionality isn't exposed by the .NET WebBrowser control. Fortunately, that control is mostly a wrapper around the 'old' control. This means you can subscribe to the BeforeNavigate2 event you know and love(?) using something like the following (after adding a reference to SHDocVw to your project):

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

...,做任何你想要的那个的PostData事件中:

...and do whatever you want to the PostData inside that event:

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基础结构,不适合直接从您的code使用。文件。换句话说:你使用该属性可能会破坏您的应用程序在未来的任何时候,例如当框架的人决定实施自己的浏览器组件,而不是包装现有的SHDOCVW COM一个

One important caveat: the documentation for the WebBrowser.ActiveXInstance property states that "This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.". In other words: your use of the property may break your app at any point in the future, for example when the Framework people decide to implement their own browser component, instead of wrapping the existing SHDocVw COM one.

所以,你不希望把这个code。在任何你运送到了很多人及/或任何应保持工作的许多框架的版本来...

So, you'll not want to put this code in anything you ship to a lot of people and/or anything that should remain working for many Framework versions to come...