HTTP POST data programmatically

R

Ratnesh Raval

Hi there,

I am trying to post data to a 3rd party page from my ASP.net page.
Here is the snippet that I use (found in most of the examples when googled)

Dim dt As String = ""
dt &= "var1=abc"
Dim rqst As Net.HttpWebRequest = Net.HttpWebRequest.Create(destURL)
rqst.AllowAutoRedirect = True
rqst.Method = Net.WebRequestMethods.Http.Post
rqst.ContentLength = dt.Length
rqst.ContentType = "application/x-www-form-urlencoded"
Dim wr As New IO.StreamWriter(rqst.GetRequestStream)
wr.Write(dt)
wr.Close()
Dim oResp As Net.HttpWebResponse = rqst.GetResponse()
'Response.Redirect(oResp.ResponseUri.ToString)
Dim reader As New IO.StreamReader(oResp.GetResponseStream())
Dim tmp As String = reader.ReadToEnd()
oResp.Close()
Response.Write(tmp)


When it posts to other page, it gets the response and displays in my current
page.

The problem: the page, where I post, is supposed to redirect to different
page(s). but this code only gets the response and displays.
Is there any way to just to go the page while posting. That way all the next
steps are taken care by other page?

Thanks,
Nesh
 
G

Guest

Hi there,

I am trying to post data to a 3rd party page from my ASP.net page.
Here is the snippet that I use (found in most of the examples when googled)

Dim dt As String = ""
dt &= "var1=abc"
Dim rqst As Net.HttpWebRequest = Net.HttpWebRequest.Create(destURL)
rqst.AllowAutoRedirect = True
rqst.Method = Net.WebRequestMethods.Http.Post
rqst.ContentLength = dt.Length
rqst.ContentType = "application/x-www-form-urlencoded"
Dim wr As New IO.StreamWriter(rqst.GetRequestStream)
wr.Write(dt)
wr.Close()
Dim oResp As Net.HttpWebResponse = rqst.GetResponse()
'Response.Redirect(oResp.ResponseUri.ToString)
Dim reader As New IO.StreamReader(oResp.GetResponseStream())
Dim tmp As String = reader.ReadToEnd()
oResp.Close()
Response.Write(tmp)

When it posts to other page, it gets the response and displays in my current
page.

The problem: the page, where I post, is supposed to redirect to different
page(s). but this code only gets the response and displays.
Is there any way to just to go the page while posting. That way all the next
steps are taken care by other page?

Thanks,
Nesh

Did you try to use Response.Redirect?
 
R

Ratnesh Raval

Alexey,

Yes, I tried it but that will just take you to destination URL.
It doesn't include form data for POST.


Hi there,

I am trying to post data to a 3rd party page from my ASP.net page.
Here is the snippet that I use (found in most of the examples when
googled)

Dim dt As String = ""
dt &= "var1=abc"
Dim rqst As Net.HttpWebRequest = Net.HttpWebRequest.Create(destURL)
rqst.AllowAutoRedirect = True
rqst.Method = Net.WebRequestMethods.Http.Post
rqst.ContentLength = dt.Length
rqst.ContentType = "application/x-www-form-urlencoded"
Dim wr As New IO.StreamWriter(rqst.GetRequestStream)
wr.Write(dt)
wr.Close()
Dim oResp As Net.HttpWebResponse = rqst.GetResponse()
'Response.Redirect(oResp.ResponseUri.ToString)
Dim reader As New IO.StreamReader(oResp.GetResponseStream())
Dim tmp As String = reader.ReadToEnd()
oResp.Close()
Response.Write(tmp)

When it posts to other page, it gets the response and displays in my
current
page.

The problem: the page, where I post, is supposed to redirect to different
page(s). but this code only gets the response and displays.
Is there any way to just to go the page while posting. That way all the
next
steps are taken care by other page?

Thanks,
Nesh

Did you try to use Response.Redirect?
 
G

Guest

I don't get it
Yes, I tried it but that will just take you to destination URL.
It doesn't include form data for POST.

Correct. It does go to destination URL
The problem: the page, where I post, is supposed to redirect to different
page(s). but this code only gets the response and displays.
Is there any way to just to go the page while posting.

I think this is what you wanted, isn't it?

If you mean that you need to 1) show output and 2) make immediate
redirect after that then it makes no big sense for the user, because
you can't see anything because of redirect. However, you can make some
delay and perform redirect using javascript, because Response.Redirect
will not help in that case. Response.Redirect does not work when
server is already sent something to the client. You can see it if you
do

Response.Write(tmp)
Response.Flush()
Response.Redirect("http://...")

Flush will show output but Redirect will fail.
 
R

Ratnesh Raval

Let me explain. This is what needs to be done.

I have page 1.aspx and there is third party page 2.aspx.

now when 1.aspx is loading ,
I need to post 2 variable (HTTP POST) say var1 and var2 to 2.aspx.

Now as I post, 2.aspx will process those variables and redirect to different
pages depending on values of the variables.

The problem with my approach is,
If I post variables and display output ( as I said in first post ). it will
just display contents of 2.aspx and the redirects done by that page will not
happen ( basically the I still have control as 1.aspx shows the content of
2.aspx)

If I post variables and then do redirect, there will be 2 seperate requests.
one with variables posted.
second without variables (the response.redirect) Thats why it will not work
as page 2.aspx will be called 2nd time without variables and it will not
know what to do.

Hope I made it clear. Thanks for replying.
 
G

Guest

Let me explain. This is what needs to be done.

I have page 1.aspx and there is third party page 2.aspx.

now when 1.aspx is loading ,
 I need to post 2 variable (HTTP POST)  say var1 and var2 to 2.aspx.

Now as I post, 2.aspx will process those variables and redirect to different
pages depending on values of the variables.

The problem with my approach is,
If I post variables and display output ( as I said in first post ). it will
just display contents of 2.aspx and the redirects done by that page will not
happen ( basically the I still have control as 1.aspx shows the content of
2.aspx)

If I post variables and then do redirect, there will be 2 seperate requests.
one with variables posted.
second without variables (the response.redirect) Thats why it will not work
as page 2.aspx will be called 2nd time without variables and it will not
know what to do.

Hope I made it clear. Thanks for replying.

Hi Ratnesh, now it's more clear, thanks. What I still don't know is
how redirect on 2.aspx is implemented, is it a javascript? You said,
you see the contents of 2.aspx. How does it look like?
 
R

Ratnesh Raval

Alexey,

2.aspx is hosted by partner. So I dont know how redirect on 2.aspx is
implemented.

I use the code on 1st post (shown below) to get the content of 2.aspx.

Dim oResp As Net.HttpWebResponse = rqst.GetResponse()
Dim reader As New IO.StreamReader(oResp.GetResponseStream())
Dim tmp As String = reader.ReadToEnd()
oResp.Close()
Response.Write(tmp)

It just appears as blank page.

I was thinking about other options like putting a button with postback url
to be 2.aspx or use javascript to post normal html form.

Thanks



Hi Ratnesh, now it's more clear, thanks. What I still don't know is
how redirect on 2.aspx is implemented, is it a javascript? You said,
you see the contents of 2.aspx. How does it look like?
 
G

Guest

Alexey,

2.aspx is hosted by partner. So I dont know how redirect on 2.aspx is
implemented.

I mean the technique, javascript, or .net... but okay, suppose it's
dotnet

when page (2.aspx) has <% Response.Redirect "http://www.msn.com"%> and
you post/get the request to that page you should get something like

HTTP/1.1 302 Object moved
Location: http://www.msn.com

If you read the response and do Response.Write(...) from 1.aspx you
will redirect your user to "http://www.msn.com".

If you get just a blank page then either you are banned or you don't
properly get the response.

To ensure what response you get you can install Fiddler
(www.fiddler2.com) and check what kind of response you application
receives as an answer from 2.aspx.

Hope this helps
 
R

Ratnesh Raval

Thanks for the tip. There are many redirects in the page (2.aspx). So I
finally decided to use Javascript to post to form then handling things
myself.

Just in case if anybody is looking for this. I used hidden form variables
and then

Dim strBuilder As StringBuilder = New StringBuilder
strBuilder.Append("form1.var1.value='abc';")
strBuilder.Append("form1.var2.value='def';")
strBuilder.Append("form1.action='" & URL & "';")
strBuilder.Append("form1.submit();")
ClientScript.RegisterStartupScript(Me.GetType, "AutoPost",
strBuilder.ToString, True)

Hope it helps
Nesh
 
Joined
Mar 25, 2010
Messages
1
Reaction score
0
Hi Ratnesh,

Could you please post the code how you actually implemented and get the data from page2.aspx redirected page
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

HTTP request with trailer 0
problem with url on http post 3
HttpWebRequest 1
HttpWebRequest 1
How to POST data 1
HTTP Post error 1
Post and redirect 8
Get url returned from request redirect 2

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top