Posting data using HttpWebRequest or WebClient RRS feed

  • Question

  • User68046565 posted

    I'm trying to post data to a page on a vb asp.net page and can't seem to get it to work. I've tried both HttpWebRequest or WebClient and it doesn't seem to send the data. I get an OK in reply, but no form data was sent. Here is what I got:


    Protected Sub PostTo(ByVal url As String, ByVal postData As String)

    Dim myWebRequest As HttpWebRequest = WebRequest.Create(url)
    myWebRequest.Method = "POST"
    Dim byteArray As Byte() = System.Text.Encoding.Default.GetBytes(postData)
    Request.ContentType = "application/x-www-form-urlencoded"
    myWebRequest.ContentLength = byteArray.Length
    Dim dataStream As IO.Stream = myWebRequest.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)
    Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
    CType(myWebResponse, HttpWebResponse).StatusDescription)
    dataStream = myWebResponse.GetResponseStream()
    Dim reader As New IO.StreamReader(dataStream)
    Dim responseFromServer As String = reader.ReadToEnd()
    Response.Write(responseFromServer + ":")

    Dim wc As New WebClient
    wc.UploadData(url, "POST", byteArray)

    End Sub

    Dim postData As String = email=abradley@gemplers.com
    PostTo(url, postData)

     Any clues would really help me out. THANKS!

    Monday, May 7, 2007 12:19 PM


  • User68046565 posted

    Disregard!!!! This line:

    Request.ContentType = "application/x-www-form-urlencoded"

    Should have been:

    myWebRequest.ContentType = "application/x-www-form-urlencoded"


    My bad

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 7, 2007 1:35 PM