Redirect with POST ?

S

Savanah

Hello,

I would like to call another page using POST method, something like this :

Response.Redirect("http://www.mydomain.com/cgi-bin/log.dll?email="+tbLoginID").

I set this in my page
<form id="form1" method="post" runat="server

But it's doen't work... :(

ASP.NET cannot call any page n POST method ?
Anyone can help me, thank in advance.

Regards,
Savanah
 
P

Peter Rilling

No. The server has nothing to do with posting. The client is the one that
packages the headers and sends the post. Using Redirect is nothing more
that if the user had typed that URL in themselves.

You can play tricks by sending the page contents down with the form fields
filled in, then using JavaScript do a submit immediately.

By the way, when you call Redirect, none of the page contents actually is
sent to the browser. The control mechanism for redirecting is a single
header field and so ASP.NET knows that there is no point in sending down any
other content.
 
S

Savanah

Thank you for your reply.

I try to use Server.Transfer but don't work too :(

How I can send URL using POST method ?
 
J

Joerg Jooss

Thus wrote Peter,
No. The server has nothing to do with posting. The client is the one
that packages the headers and sends the post. Using Redirect is
nothing more that if the user had typed that URL in themselves.

That's probably popular belief, but not true.

HTTP defines status code 307 (Temporary Redirect) to redirect a POST as POST.
The run-of-the-mill redirect everybody knows OTOH is 302
(Found), which is what you get if you call HttpResponse.Redirect(). To make
a sad story short, a 302 blindly turns POST into GET (first it was bug, today
it's a feature).

But it's not hard to implement a 307 redirect in ASP.NET:

public void RedirectTemporary(string url) {
Response.ClearContent();
Response.StatusCode = 307;
Response.StatusDescription = "Temporary Redirect";
Response.RedirectLocation = ResolveClientUrl(url); // this assumes url
is relative, like "~/PathTo/WebForm.aspx"
Response.Flush();
}

Note: According to the HTTP 1.1 spec, a browser should warn the user before
redirecting a POST request. Firefox follows the spec, IE6 doesn't.

Cheers,
 
P

Peter Rilling

I learned something, cool.

Joerg Jooss said:
Thus wrote Peter,


That's probably popular belief, but not true.
HTTP defines status code 307 (Temporary Redirect) to redirect a POST as
POST. The run-of-the-mill redirect everybody knows OTOH is 302 (Found),
which is what you get if you call HttpResponse.Redirect(). To make a sad
story short, a 302 blindly turns POST into GET (first it was bug, today
it's a feature).

But it's not hard to implement a 307 redirect in ASP.NET:

public void RedirectTemporary(string url) {
Response.ClearContent();
Response.StatusCode = 307;
Response.StatusDescription = "Temporary Redirect";
Response.RedirectLocation = ResolveClientUrl(url); // this assumes url is
relative, like "~/PathTo/WebForm.aspx"
Response.Flush();
}

Note: According to the HTTP 1.1 spec, a browser should warn the user
before redirecting a POST request. Firefox follows the spec, IE6 doesn't.

Cheers,
 
J

Joerg Jooss

Thus wrote Peter,
I learned something, cool.

You're welcome :)

I just wish the ASP.NET product team would include this as another HttpResponse.Redirect()
overload:

HttpResponse.Redirect(string url, bool endResponse, HttpRedirectStatus status)

Cheers,
 
J

Joerg Jooss

Thus wrote Joerg,
But it's not hard to implement a 307 redirect in ASP.NET:

public void RedirectTemporary(string url) {
Response.ClearContent();
Response.StatusCode = 307;
Response.StatusDescription = "Temporary Redirect";
Response.RedirectLocation = ResolveClientUrl(url); // this assumes
url
is relative, like "~/PathTo/WebForm.aspx"
Response.Flush();
}

Important note: The receiving web form must apply the directive EnableViewStateMac="false",
otherwise Machine Authentication Check blows up in your face.

Cheers,
 
S

Savanah

Thank you Joerg Joos.

But you method don't work with .NET v1.1, I got error on ResolveClientUrl
function

I'm trying to use Server.Transfer like this :

Server.Transfer("http://localhost/cgi-bin/webengine.dll/log?email="+tbMail.Text+"&pw="+tbPassword.Text");
// Server redirection

I got error message :
"Error executing child request for
"http://localhost/cgi-bin/webengine.dll/log?email="+tbMail.Text+"&pw="+tbPassword.Text"

Why ? I can access at this page when copy and paste URL into IE ...

Regards,
Savanah
 
J

Joerg Jooss

Thus wrote Savanah,
Thank you Joerg Joos.

But you method don't work with .NET v1.1, I got error on
ResolveClientUrl function

Yes, this a .NET 2.0 API.
I'm trying to use Server.Transfer like this :

Server.Transfer("http://localhost/cgi-bin/webengine.dll/log?email="+tb
Mail.Text+"&pw="+tbPassword.Text");

// Server redirection

I got error message :
"Error executing child request for
"http://localhost/cgi-bin/webengine.dll/log?email="+tbMail.Text+"&pw="
+tbPassword.Text"
Why ? I can access at this page when copy and paste URL into IE ...

That cannot work. HttpServerUtility.Transfer() only works within an ASP.NET
application, and has *nothing* to do with HTTP redirects. If you cannot use
ResolveClientUrl(), just remove the call and use absolute URLs with the method
I presented.

Cheers,
 
J

Joerg Jooss

Thus wrote Savanah,
so ASP.NET is unable to make simple page redirection using Post method
?!!

*Please* read what I've written.

If you want to use the code I've presented with .NET 1.1, you need to drop
the line with ResolveClientUrl() and simply work with absolute URLs, e.g.
"http://host/PathTo/WebForm.aspx" instead of "~/PathTo/WebForm.aspx". Or
is there another problem I'm missing?

Cheers,
 
Joined
Sep 3, 2008
Messages
1
Reaction score
0
Thanks for the tip.
But there is a way to add parameters.
I don't want to put directly on the url because I have an url
with more than 2,083 characters
Thanks in advance.
 
Joined
Sep 4, 2009
Messages
1
Reaction score
0
Redirect with post

I found this thread while I was trying to figure out how to programatically do a post to PayPal checkout page. I tried httpwebrequest and webclient and the redirect method posted here but could not get anything to work. Then I figured out a way to make it work.

put an aspnet image button on the form and use the paypal graphic. Then when the user clicks submit, you can read the values off of the controls and then register them with clientscript.registerhiddenfield("cmd","_x-click") for example.

Then in code change the form action with me.form1.action and change it to the paypal webscr script and change the method with me.form1.method="POST"

Next create a string builder to hold the startup javascript and then use clientscript.registerstartupscript to cause the form to automatically redirect to PP with the hidden fields populated.

Example:

Me.form1.Action = "cant post URL but use the one for webscr @ paypal"
Me.form1.Method = "POST"
Dim sbScript As New Text.StringBuilder

sbScript.AppendLine("<script type=""text/javascript"">")
sbScript.AppendLine("document.form1.submit()")
sbScript.AppendLine("</script>")

ClientScript.RegisterStartupScript(Me.GetType, "Redir", sbScript.ToString)

Hope this helps someone.

Galen
 
Joined
Jan 3, 2010
Messages
2
Reaction score
0
about your post to redirect a page using post

HI james

I am using a webform which contains some ajax controls. In that page had set some hidden fields and on click of a button i want to save data to the database
and send this information to a third party server.

I am trying to post this using your post but it is throwing an error such as
PageRequestManagerParserException error parsing near <!doctype ht'
etc.

Can you address this problem or suggest me some solution to this problem ?
 
Joined
Jan 3, 2010
Messages
2
Reaction score
0
NumbLock said:
I found this thread while I was trying to figure out how to programatically do a post to PayPal checkout page. I tried httpwebrequest and webclient and the redirect method posted here but could not get anything to work. Then I figured out a way to make it work.

put an aspnet image button on the form and use the paypal graphic. Then when the user clicks submit, you can read the values off of the controls and then register them with clientscript.registerhiddenfield("cmd","_x-click") for example.

Then in code change the form action with me.form1.action and change it to the paypal webscr script and change the method with me.form1.method="POST"

Next create a string builder to hold the startup javascript and then use clientscript.registerstartupscript to cause the form to automatically redirect to PP with the hidden fields populated.

Example:

Me.form1.Action = "cant post URL but use the one for webscr @ paypal"
Me.form1.Method = "POST"
Dim sbScript As New Text.StringBuilder

sbScript.AppendLine("<script type=""text/javascript"">")
sbScript.AppendLine("document.form1.submit()")
sbScript.AppendLine("</script>")

ClientScript.RegisterStartupScript(Me.GetType, "Redir", sbScript.ToString)

Hope this helps someone.

Galen


can u post a c# script for the same above ?
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top