Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > VB.NET > Windows Forms

Windows Forms Discussion related to Winforms application development

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-07-2008, 1:24 PM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Dec 2008
Posts: 13
Reputation: 18
cmd_17 is on a distinguished programming path ahead
Default Problem with programmatically submitting a form to web page

Hello,

I'm an ASP.NET developer attempting to make my first Windows Forms application, and it's not going so well.

What I'm trying to do is programmatically fill in and submit an online form via a WebBrowser Control. I've researched this for hours already and found some example code, but nothing for my particular situation except the code found here: All About Microsoft.Net Technologies!: Programmatically input data on a form and click submit button using C#

I'm getting a NullReferenceException ("object reference not set to an instance of an object") for each of the two lines (which I tested separately) after WebBrowser1.Navigate(startSite). I've dealt with this error in ASP.NET, but I don't understand what the problem is in this particular context.

Here's my code:

------------------ Form1.vb ------------------
Code:
Public Class Form1

    Const startSite As String = "http://www.somesite.com/somepage.aspx"

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        WebBrowser1.Hide()
        WebBrowser1.Navigate(startSite)
        WebBrowser1.Document.GetElementById("TextBox1").SetAttribute("Text", TextBox1.Text)
        WebBrowser1.Document.GetElementById("Button1").InvokeMember("click")

    End Sub
End Class
------------------ somepage.aspx ------------------

(some code omitted for brevity)

Code:
<form id="form1" runat="server">
    <div>
        Enter your email:<br />
        <br />
        <asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox><br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Submit" /></div>
    </form>
I'd like the user to enter their email into a TextBox on Form1, press a button, and have an invisible WebBrowser navigate to the desired page and submit the form.

Any help with this would be greatly appreciated. Thanks in advance.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 12-07-2008, 4:25 PM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,325
Reputation: 1315
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
Default

It is too soon to do anything right after the Navigate call, after that a lot happens with the browser. I'm sure you've witnessed anything from short to long delays before a document display when you are browsing the internet, also called "load time". What happens is that the browser connects to the other site and request a document, which it download and parses the content for, any other resources specified in the document source like script files, style sheets and images is downloaded, things are finally rendered to the combination of elements that displays in the browser window. The Navigate call does not block and wait until all this is done before returning, it returns immediately and will notify clients like you about changes in the browser state using events. What you have to do is to listen to the DocumentCompleted event, here you must check the browsers ReadyState property and see that it reports "Complete" before you attempt to access the Document tree programmatically.
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-07-2008, 7:03 PM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Dec 2008
Posts: 13
Reputation: 18
cmd_17 is on a distinguished programming path ahead
Default

Thanks for your helpful reply, John. It sounds like I need to do some more reading on the WebBrowser Control.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-07-2008, 11:37 PM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Dec 2008
Posts: 13
Reputation: 18
cmd_17 is on a distinguished programming path ahead
Default

It turns out that the task is easier to accomplish with an HttpWebRequest. The working code, for anyone who's interested, is as follows:

Code:
Public Class Form1

    Const postPage As String = "http://www.somesite.com/cgi-bin/whatever.cgi"

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        SubmitForm()
    End Sub

    Private Sub SubmitForm()
        Dim headerVars As String = "email=" & TextBox1.Text & "&item2=value2&item3=value3"
        Dim bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(headerVars)
        Dim req As HttpWebRequest = DirectCast(WebRequest.Create(postPage), HttpWebRequest)
        req.Method = "POST"
        req.ContentType = "application/x-www-form-urlencoded"
        req.ContentLength = bytes.Length

        Using requestStream As Stream = req.GetRequestStream()
            requestStream.Write(bytes, 0, bytes.Length)
        End Using

        Using response As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
            If response.StatusCode <> HttpStatusCode.OK Then
                Dim message As String = String.Format("POST failed. Received HTTP {0}", response.StatusCode)
                Throw New ApplicationException(message)
            Else
                MessageBox.Show("Success!")
            End If
        End Using
    End Sub

End Class
References:

this.Reflect() | Automated Form Posting with .NET / ASP.NET's CURL

Sending XML via HttpWebRequest - microsoft.public.dotnet.languages.csharp | Google Groups
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 11:50 PM.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.