View Single Post
  #4 (permalink)  
Old 12-07-2008, 11:37 PM
cmd_17 cmd_17 is offline
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
Reply With Quote