How to get POSTDATA from webbrowser object in VB.NET 2010?

1.9k views Asked by At

I understand in previous versions of VB the webbrowser object had a beforenavigate2 event that provided access to the postdata of the webbrowser.

I've searched and searched and I think that event was disabled for visual studio 2010.

Any thoughts on how I could grab postdata from the webbrowser object?

1

There are 1 answers

0
dcontard On

I know this is an old question, but I was struggling with the same question and now thought of sharing the answer here. Credits for this answer goes to luchosrock since he was the one that teached it to me.

If you have an instance of the System.Windows.Forms.WebBrowser object named, say, browser, you can implement your own handler to control the Navigating event (that is somehow equivalent to BeforeNavigate2) and use the System.Net.WebRequest and System.Net.WebResponse objects from within it:

Imports System.IO
Imports System.Net
Imports System.Windows.Forms
'[...]
Private Sub browser_Navigating(sender As Object, _ 
                               e As WebBrowserNavigatingEventArgs) _ 
                           Handles browser.Navigating

    Dim req As WebRequest
    Dim res As WebResponse
    Dim postDataStream As Stream
    Dim WebResponse As String = ""
    '[...]
    Try
        req = WebRequest.Create(e.Url.ToString)
        req.Method = "POST"
        res = req.GetResponse
        postDataStream = res.GetResponseStream
        Dim webStreamReader As New StreamReader(postDataStream)
        While webStreamReader.Peek >= 0
            WebResponse = webStreamReader.ReadToEnd
        End While
    Catch ex As Exception
        ' Exception control code here
    End Try
    '[...]
End Sub

It is not the same as accessing directly the PostData object from within the BeforeNavigating2 event, but is a nice alternative, I think.

If this answer doesn't satisfices you, there is this answer in another question in which a method is explained to handle directly the old BeforeNavigate2 event.