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?

1

4 Answers 4

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();
Sign up to request clarification or add additional context in comments.

5 Comments

useful trick. isn't there any straightforward way to send values through POST method though?
Is the user, at any stage, able to view source (or similar) and see what post data is passed?
What if we need to add cookies aswell to make the post work on the external site?
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)
You can't add an extra form tag to an ASPX page. I provided an alternative solution here: stackoverflow.com/a/12885706/31444
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.

7 Comments

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, 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.
About your last comment: Client browser should transmit data to another server. Suggestion: What about clearing headers, add POST headers and then redirect?
@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.
+1 Thanks for good suggestions. Govind solution which you mentioned fits my scenario.
|
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.

Comments

0

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

1 Comment

Not on a response.redirect("destination", false) - it's transferred using HTTP 301; no page is rendered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.