By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Reading PostData from WebBrowser

100+
P: 116
Hi

I have a WebBrowser on a form. On loading, the browser finds an online login page.

After logging in via this page some postdata is generated, say forename and surname.

There should be a way to read this via BeforeNavigate2 but nothing seems to work. I've tried using WebBrowser and AxWebBrowser but can't find any code that reads the data. Outputting the URL doesn't help as the post data isn't part of the URL.

Apparently BeforeNavigate2 only works on AxWebBrowser in 2010 which only increases the confusion.

Anyone have any simple known working code that can grab the post data from an embedded browser in vb2010?

Many thanks
Jul 1 '14 #1
Share this Question
Share on Google+
4 Replies


100+
P: 299
I actually used code from here http://stackoverflow.com/questions/1...in-vb-net-2010 and it seemed to work fine. I've included the code I used for a quick copy & paste so you can see if it suits your needs.

All I did in advance was set my WebBrowser1 URL in the properties window and added a multi-line textbox to see the output.

Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2. Imports System.Net
  3. Imports System.Windows.Forms
  4.  
  5.  
  6. Public Class Form1
  7.     Private Sub WebBrowser1_Navigating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
  8.         Dim req As WebRequest
  9.         Dim res As WebResponse
  10.         Dim postDataStream As Stream
  11.         Dim webResponse As String = ""
  12.  
  13.         Try
  14.             req = WebRequest.Create(e.Url.ToString)
  15.             req.Method = "POST"
  16.             res = req.GetResponse
  17.             postDataStream = res.GetResponseStream
  18.             Dim webStreamReader As New StreamReader(postDataStream)
  19.             While webStreamReader.Peek >= 0
  20.                 webResponse = webStreamReader.ReadToEnd
  21.             End While
  22.             TextBox1.Text = webResponse.ToString
  23.         Catch ex As Exception
  24.             'do something for exceptions
  25.         End Try
  26.     End Sub
  27. End Class
Jul 1 '14 #2

100+
P: 116
Thx Luk3r,

I think I actually had a try with this but it seemed to output the code for the page rather than just the post data.

However I'll have a look tomorrow to see if that was a mistake on my part.

Just to clarify, if the online login page response posts ?forename=Bill&surname=Smith that's all I need to read.

Will see how it goes and will let you know once I've had time to have a look.

Many thanks
Jul 1 '14 #3

100+
P: 116
I can confirm that this method grabs the page source rather than just postdata unfortunately.
Jul 2 '14 #4

100+
P: 116
Ok I finally found something that works - but only because I know the name of the element I require. Not sure how to do this without that knowledge but for my purposes the following works.

Expand|Select|Wrap|Line Numbers
  1. Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
  2.         Dim thiselm As HtmlElement
  3.         thiselm = WebBrowser1.Document.GetElementById("surname")
  4.         If Not thiselm = Nothing Then TextBox1.Text = Uri.EscapeUriString(thiselm.GetAttribute("value").ToString)
  5.     End Sub
  6.  
Jul 2 '14 #5

Post your reply

Sign in to post your reply or Sign up for a free account.