• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

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

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
0
goknows
Asked:
goknows
  • 3
1 Solution
 
jfoutzCommented:
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...
0
 
jfoutzCommented:
I forgot to mention...

Namespaces to import are System.Net and System.IO
0
 
goknowsAuthor Commented:
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" />
</form>

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

Thanks in Advance
0
 
jfoutzCommented:
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.Clear()
Response.Buffer = True
Response.BinaryWrite(objRetBytes)
Response.End()
Response.Flush()

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.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now