Response.Redirect with POST instead of Get? Response.Redirect with POST instead of Get? asp.net asp.net

Response.Redirect with POST instead of Get?


Doing this requires understanding how HTTP redirects work. When you use Response.Redirect(), you send a response (to the browser that made the request) with HTTP Status Code 302, which tells the browser where to go next. By definition, the browser will make that via a GET request, even if the original request was a POST.

Another option is to use HTTP Status Code 307, which specifies that the browser should make the redirect request in the same way as the original request, but to prompt the user with a security warning. To do that, you would write something like this:

public void PageLoad(object sender, EventArgs e){    // Process the post on your side       Response.Status = "307 Temporary Redirect";    Response.AddHeader("Location", "http://example.com/page/to/post.to");}

Unfortunately, this won't always work. Different browsers implement this differently, since it is not a common status code.

Alas, unlike the Opera and FireFox developers, the IE developers have never read the spec, and even the latest, most secure IE7 will redirect the POST request from domain A to domain B without any warnings or confirmation dialogs! Safari also acts in an interesting manner, while it does not raise a confirmation dialog and performs the redirect, it throws away the POST data, effectively changing 307 redirect into the more common 302.

So, as far as I know, the only way to implement something like this would be to use Javascript. There are two options I can think of off the top of my head:

  1. Create the form and have its action attribute point to the third-party server. Then, add a click event to the submit button that first executes an AJAX request to your server with the data, and then allows the form to be submitted to the third-party server.
  2. Create the form to post to your server. When the form is submitted, show the user a page that has a form in it with all of the data you want to pass on, all in hidden inputs. Just show a message like "Redirecting...". Then, add a javascript event to the page that submits the form to the third-party server.

Of the two, I would choose the second, for two reasons. First, it is more reliable than the first because Javascript is not required for it to work; for those who don't have it enabled, you can always make the submit button for the hidden form visible, and instruct them to press it if it takes more than 5 seconds. Second, you can decide what data gets transmitted to the third-party server; if you use just process the form as it goes by, you will be passing along all of the post data, which is not always what you want. Same for the 307 solution, assuming it worked for all of your users.

Hope this helps!


You can use this aproach:

Response.Clear();StringBuilder sb = new StringBuilder();sb.Append("<html>");sb.AppendFormat(@"<body onload='document.forms[""form""].submit()'>");sb.AppendFormat("<form name='form' action='{0}' method='post'>",postbackUrl);sb.AppendFormat("<input type='hidden' name='id' value='{0}'>", id);// Other params go heresb.Append("</form>");sb.Append("</body>");sb.Append("</html>");Response.Write(sb.ToString());Response.End();

As result right after client will get all html from server the event onload take place that triggers form submit and post all data to defined postbackUrl.


HttpWebRequest is used for this.

On postback, create a HttpWebRequest to your third party and post the form data, then once that is done, you can Response.Redirect wherever you want.

You get the added advantage that you don't have to name all of your server controls to make the 3rd parties form, you can do this translation when building the POST string.

string url = "3rd Party Url";StringBuilder postData = new StringBuilder();postData.Append("first_name=" + HttpUtility.UrlEncode(txtFirstName.Text) + "&");postData.Append("last_name=" + HttpUtility.UrlEncode(txtLastName.Text));//ETC for all Form Elements// Now to Send Data.StreamWriter writer = null;HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);request.Method = "POST";request.ContentType = "application/x-www-form-urlencoded";                        request.ContentLength = postData.ToString().Length;try{    writer = new StreamWriter(request.GetRequestStream());    writer.Write(postData.ToString());}finally{    if (writer != null)        writer.Close();}Response.Redirect("NewPage");

However, if you need the user to see the response page from this form, your only option is to utilize Server.Transfer, and that may or may not work.