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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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();
Method 2
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.
Method 3
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.
- Collect user inputs in a.aspx. User clicks submit button which causes postback.
- 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.
- Encrypt the final name-value string and set it to a cookie.
- Redirect to b.aspx
- 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.
Method 4
You can use viewstate to “transfer” the data and read it on a new page or even the same page.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0