Include post variables in a createDocumentFromUrl call in .NET

654 views Asked by At

I have a project in Visual Basic 2010 Express that parses web pages use the IHTMLDocument object. Here is the function I am using to retrieve a web page:

Private Function GetHTML(ByVal url As String)
    Dim htmldoc As New HTMLDocument()
    Dim ihtml2 As IHTMLDocument2
    Dim ihtml3 As IHTMLDocument3
    Dim iPersistStream As IPersistStreamInit

    iPersistStream = DirectCast(htmldoc, IPersistStreamInit)
    iPersistStream.InitNew()

    ihtml2 = htmldoc.createDocumentFromUrl(url, vbNullString)

    Do Until ihtml2.readyState = "complete"
        'required for htmlresult.readyState to transition from "loading" to "complete"
        System.Windows.Forms.Application.DoEvents()
    Loop
    ihtml3 = DirectCast(ihtml2, IHTMLDocument3)

    Return ihtml3
End Function

I'm basically doing stuff like this with the function:

ihtml = GetHTML("www.blah.com?getvar1=x&getvar2=y")
ihtml.getElementsByTagName("A")
ihtml.getElementById("myel")
etc, etc...

I'm trying to figure out how I can include POST variables in addition to the URL string when I retrieve the HTML document. By which I mean I'd like to be able to something like this:

ihtml = GetHTML("www.blah.com?getvar1=x&getvar2=y",["postvar1=a","postvar2=b"])

So I'd like to modify my existing GetHTML function in order to allow me to include post variables if that is possible and if not I'd like to know if there is perhaps another way to do this. Thanks to anyone who can help.

1

There are 1 answers

0
MatthewMartin On

Ok, I would have used WebCilent or WebRequest but... if you're committed to this design:

You have to navigate to the page, find each input field, set the value then call InvokeMember on the button for that page. WebBrowser is all about automating a web browser, not so much about making HTTP requests programatically.

Sample code: http://muftisubzero.blogspot.com/2010/12/playing-with-webbrowser-class-cnet.html