webrequest error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • devquest
    New Member
    • Dec 2009
    • 12

    webrequest error

    If I post a html form like this:
    <form action="htpp://url.com" method="post" />
    <input type="hidden" name="name" value="John Doe" />
    <input type="hidden" name="address" value="222 Main St" />
    <input type="submit" />
    it sends the data successfully.

    But when I try to do this in vb:
    Code:
    writer.Append("name=john doe&address=222Main St")
    Dim webresponse As WebResponse = Nothing
    Dim url As String = "http://url.com"
    Dim req As WebRequest = WebRequest.Create(url)
    req.Method = "POST"
    req.ContentType = "application/x-www-form-urlencoded"
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(writer.ToString)
    req.ContentLength = byteArray.Length
    Dim reqStream As Stream = req.GetRequestStream()
    reqStream.Write(byteArray, 0, byteArray.Length)
    reqStream.Close()
    I get an error (500 internal server error)
    Am I doing this correctly?
    thank you.
  • JamieHowarth0
    Recognized Expert Contributor
    • May 2007
    • 537

    #2
    Hi @devquest,

    You've got a space in "name=john doe". You need to urlencode all the values passed in the HTTP header (so a space turns into %20) otherwise the server thinks you've reached the end of the header. Your whole header should look like this:
    Code:
    writer.Append("name=john%20doe&address=222Main St")
    Best,

    codegecko

    Comment

    Working...