18

I want to redirect a response to another URL while it contains some POST data in it's HTTP header.

// Inside an ASP.NET page code behind:
Response.Redirect("http://www.example.com/?data=sent%20via%20GET");
// This will sent data to http://www.example.com via GET.
// I want to POST this data to http://www.example.com instead.

How to do this in ASP.NET?

Xaqron
  • 29,931
  • 42
  • 140
  • 205
  • possible duplicate of [Response.Redirect with POST instead of Get?](http://stackoverflow.com/questions/46582/response-redirect-with-post-instead-of-get) – tghw Dec 18 '12 at 01:46

4 Answers4

57

you can send huge data also with this trick..

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 here
sb.Append("</form>");
sb.Append("</body>");
sb.Append("</html>");

Response.Write(sb.ToString());

Response.End();
Govind Malviya
  • 13,627
  • 17
  • 68
  • 94
  • useful trick. isn't there any straightforward way to send values through POST method though? – rDroid Apr 26 '13 at 09:10
  • Is the user, at any stage, able to view source (or similar) and see what post data is passed? – Nostradamus Sep 16 '13 at 03:26
  • What if we need to add cookies aswell to make the post work on the external site? – confusedMind Aug 13 '14 at 14:01
  • This was exactly what i needed.. i had tried server.transfer (only good within a website, and response.redirect (and that was a no go).. WebRequest.. not even close.. The dynamic building and forwarding from the response.clear to response.end was EXACTLY what i needed to solve my issue. (i needed to forward query string and form data to a different page for debugging) – roblem May 23 '17 at 19:27
  • You can't add an extra form tag to an ASPX page. I provided an alternative solution here: https://stackoverflow.com/a/12885706/31444 – IrishChieftain Jul 28 '23 at 14:15
13

You can't POST using a redirect. By definition a redirect means the server sending a 302 redirect HTTP status code to the client with the new location so that the client issues a GET request to this new location. That's how the HTTP protocol works and there's not mu ch you can do about it.

So one way to achieve this would be to redirect to some temporary page that will contain an HTML <form> with method="POST" and containing the values you want to send as hidden fields. Then you could use javascript to autosubmit this form.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • My data is large and beyond 4096 bytes which is the maximum current browsers can handle. How to post this data without java-script? – Xaqron May 19 '11 at 17:03
  • @Xaqron, two possibilities: you create an HTML form and put the data in hidden fields inside this form and then submit the form or you do the request server side using a WebRequest. – Darin Dimitrov May 19 '11 at 17:04
  • About your last comment: Client browser should transmit data to another server. Suggestion: What about clearing headers, add POST headers and then redirect? – Xaqron May 19 '11 at 17:06
  • @Xaqron, as I said in my answer redirect => GET. There's something else you could try. Persist this data into some common data store and redirect to the remote url by passing only an id so that this remote url will be able to fetch the data back from the common data store using this id. – Darin Dimitrov May 19 '11 at 17:07
  • +1 Thanks for good suggestions. `Govind` solution which you mentioned fits my scenario. – Xaqron May 19 '11 at 17:18
  • I know this is an old post, but @DarinDimitrov could you improve it with POST resolution at this time? I make the question as: http://stackoverflow.com/questions/26868821/post-and-redirect-from-web-api – Leandro Bardelli Nov 11 '14 at 16:28
  • It is 2018 and I wonder if there is still no way of doing this? – Neville May 17 '18 at 15:12
0

Though it is quite old thread, I thought I would share how I do it.

Let's say sending page is a.aspx and destination page is b.aspx.

  1. Collect user inputs in a.aspx. User clicks submit button which causes postback.
  2. inside Button_click event of a.aspx, process volume data (for example save uploaded binary files). Determine link of the volume data and append that to end of request.form name-value string.
  3. Encrypt the final name-value string and set it to a cookie.
  4. Redirect to b.aspx
  5. in b.aspx, retrieve that cookie, decrypt and you get all name-value pair. Now process them as usual.

Advantages: (a) b.aspx is shown in browser address bar. It enters in browser's history. Server.transfer do these. (b) It gives effect of post. Users can not see the name-value pair in querystring.

Kish
  • 51
  • 1
  • 3
0

You can use viewstate to "transfer" the data and read it on a new page or even the same page.

Liron Harel
  • 10,819
  • 26
  • 118
  • 217
  • Not on a `response.redirect("destination", false)` - it's transferred using HTTP 301; no page is rendered. – GlennG Mar 22 '17 at 14:24