POST http request using WebBrowser

Megalith

Well-known member
Joined
Aug 21, 2006
Messages
66
Programming Experience
10+
i am trying to automate a few functions on a server i visit and have setup a form with a webbrowser on it, I believe i need to use a POST http request to do this but cant find how to use this within the webbrowser. I am using .NET2 to write this application. should i continue to use webbrowser to do this? in essence i need to click a specific item on the page and then click ok.

The site uses PHP if this helps.
 
Tell the browser to nav to the page with the form
Programmatically fill in the form (webBroswer1.Document.Forms("whatever").whatever..)
Programmatically submit the form..

Web browser will nav to the new page..
 
it turned out the solution to the problem was very simple and i created a URL that does the required choice. When however i tried to move the application onto the next url the application fails heres what i'm trying to do
VB.NET:
Private Sub OkClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OkClick.Click
WebBrowser1.Navigate(page1+post.text)
My.Application.DoEvents()
WebBrowser1.Navigate(page2)
End Sub


page 1 is the page that uses the post data i need and page 2 is the homepage. If i insert a msgbox between the 2 webbrowser1 lines the routine works but without it as above the routine misses the first URL completely. Initially i had the 2nd url in the WebBrowser1.DocumentCompleted event and it worked but i thought the code looked ugly. The inclusion of My.Application.DoEvents() was i hoped a solution but not so.

Does anyone have any suggestions on how this code would work as intended?
 
it turned out the solution to the problem was very simple and i created a URL that does the required choice.

Yeah, that does a GET request, you explicitly said you needed a POST request.. and if you were sending to any server I wrote, and it was expecting a post and you sent it a get.. youd be shown to the digital door :D


WebBrowser1.DocumentCompleted event and it worked but i thought the code looked ugly.
Ugly works, but beautiful doesnt? :D

Ugly works because you have to wait for page 1 to load before jumping to page 2. this is because in the response of page 1, you get sent a cookie that keeps you logged in. No response=no cookie=no go.

The inclusion of My.Application.DoEvents() was i hoped a solution but not so.
People really, really, really need to get away from that naaasty old vb6 way of doing things. In vb6, we had doevents because it was single threaded, so any long running ops would mean that the thread that processes the window messages(mouse clicks, key presses) would be busy doing other things and the app would (look like it) "crashed"
Inserting a DoEvents every now and then in the longop meant that the thread would stop work, process all the queued up window messages and not look like it crashed.

Now that we have multi-threading, long ops are suppsoed to be done on a background thread so the windowing thread can still process its messages and not look like the app crashed.

Now that i've given youa brief history of DoEvents, you can hopefully see how:
a) it has nothing to do with your app "doing all the work you told it to do like navigating a web browser"
b) it is only purely to do with the window messages sent to it by the user mouse, and keyboard (and windows dummy messages to see if it is "Not Responding")
c) given that its something of a no-no and hence almost like a swear word in your code, it might just be uglier than the other route!

Does anyone have any suggestions on how this code would work as intended?
the "ugly" way...

:D
 
incidentally, web browser has a Navigate() overload that allows you to give it POST data.. you do not tag it onto the url..

plus.. what's this:

page1+post.text <-- + in vb is used for numbers, not strings?!
 
I changed the code back to how i had it initially and it functions correctly, it works but as i said it just doesnt look slick. I've also replaced the '+' with '&' i find that the '+' looks right which is why i use it tho technically even back in the qbasic days the '+' would have inserted an unwanted space in the code. Its one of those things i need to get used to doing :)

Thanks for the suggestions cjard. intuitively i can do oop which is promising i should stick with what works & not what looks good i know, but i do like my code to look good even if the end user doesn't know it lol
 
Back
Top