views:

2904

answers:

2

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?

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.

+2  A: 

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

...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

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.

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...

mdb
Thank you. I feared this might be the answer. We specifically moved to the new WebBrowser so we wouldn't have to reference the dreaded shdocvw anymore. So, this really isn't workable for us, even though it is the correct answer.
awhite
A: 

I wonder why they don't have the post data in neither the WebBrowserNavigatingArgs nor WebBrowserNavigationArgs.

Anyone know how Microsoft expects developers to handle this in the future?

Helgi Hrafn Gunnarsson