need to simulate a form submit with action=post from 2.0 (vb)

Posted on 2006-04-11
Last Modified: 2008-01-09
I'm working with a vendor who require I send info through a form post.  I cannot just stick in an asp classic page with a html form as there is some database interaction AFTER the user has completed the form that provides one of the values to be sent.

I think this uses the HTTPRequest Object but ... Sample code would be great.

Thanks in advance
Question by:goknows
    LVL 1

    Accepted Solution

    HTTPRequest is one way to do it, but a more common way is with Webclient.  It's hard to provide sample code when I don't know what the purpose of the request is.  i.e. upload data, download data, what type of data, etc.  The sample below takes a string for posting and returns a string:

    Dim Address As String = "http://..."
    Dim objInf As String = "..."
    Dim objBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(objInf)
    Dim objRequest As New WebClient
    Dim objRetBytes As Byte()
    Dim objRetInf As String
    objRetBytes = objRequest.UploadData(Address, "POST", objBytes)
    objRetInf = System.Text.Encoding.ASCII.GetString(objRetBytes)

    Similarly, if you're working with name-value collections:

    Dim objRequest As New WebClient()
    Dim objInf As New System.Collections.Specialized.NameValueCollection(1)
    Dim objRetBytes() As Byte
    Dim objRetVals() As String
    objRequest.BaseAddress = "http://..."
    objInf.Add("key", "value")
    objRetBytes = objRequest.UploadValues(objRequest.BaseAddress, "POST", objInf)
    objRetVals = System.Text.Encoding.ASCII.GetString(objRetBytes).Split(",".ToCharArray())

    Hopefully you're getting the idea...
    LVL 1

    Expert Comment

    I forgot to mention...

    Namespaces to import are System.Net and System.IO

    Author Comment

    This does work and get's me half way there ... I need to not only submit the data (name-values pairs), but also navigate to the page (with my values in the header) exactly as if the had clicked a submit button on an html form

    I want to mimic the functionality of a form like this:
    <form id="sas" action="post">
    <input id="somekey" name="somekey" type="hidden" value="boo">
    <input id="Submit1" type="submit" value="submit" />

    but programatically populate the header info and the go there in a server side script.

    Thanks in Advance
    LVL 1

    Expert Comment

    Hmmm...  I can't think of a way to do that exactly.

    Here's a thought though:

    Dim objRequest As New WebClient()
    Dim objInf As New System.Collections.Specialized.NameValueCollection(1)
    Dim objRetBytes() As Byte
    objRequest.BaseAddress = "http://..."
    objInf.Add("key", "value")
    objRetBytes = objRequest.UploadValues(objRequest.BaseAddress, "POST", objInf)
    Response.Buffer = True

    I haven't tested this code, so there's probably a syntax error or two in it, but you get the idea.

    One BIG problem with this code though.  The location in the browser will still reflect that of the original page, not the page you redirected to.  If you don't care about that, this will work.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now