3

I need to redirect a user to http://www.someurl.com?id=2 using a POST method. Is it possible? If yes, then how?

Right now I have following and it forwards the POST data properly, but it removes the ?id=2:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.someurl.com?id=2");
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;

using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
    writer.Write(postData);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    Response.Write(reader.ReadToEnd());
}

The reason I need both query string data -> ?id=2 and POST data is because I pass the query string to page in which javascript is going to be handling the query string data and .NET is going to work with data sent via POST method. The POST data that I am passing can be longer than maximum amount of characters that GET method allows, therefore I can't use only GET method... so, what are your suggestions?

More information: I am writing a routing page, which adds some custom information to query string and then routes all of the data, old and new further to some URL that was provided. This page should be able to redirect to our server as well as to someone's server and it doesn't need to know where it came from or where it goes, it just simply needs to keep the same POST, GET and HEADER information as well as additional information received at this step.

Victor F
  • 917
  • 16
  • 30
  • 1
    Apache accepts POST requests with GET parameters, at least, so chances are the HTTP protocol allows it. Therefore, I suppose it's possible. – zneak Nov 01 '10 at 18:46
  • 1
    It is possible: http://plato.cs.virginia.edu/~tfs3p/getpost.html. Unfortunately I am not familiar with .NET so I don't know exactly what the problem is. Are you sure that `urlReferrer.ToString()` returns the URL with the GET variables, rather than just the URL? – Tom Smilack Nov 01 '10 at 19:09
  • request.Referer = urlReferrer.ToString(); is not that important in this case. The place where I am showing where to redirect is at (HttpWebRequest)WebRequest.Create(redirectUrl); – Victor F Nov 01 '10 at 19:16
  • Tom, this is pretty much exactly what I need! – Victor F Nov 01 '10 at 19:27
  • 1
    Related: http://stackoverflow.com/questions/611906/http-post-with-url-query-parameters-good-idea-or-not – Josh Stodola Nov 01 '10 at 20:52

8 Answers8

2

No. There is no fathomable reason to mix POST and GET.

If you need to make parameters passed in with the request available to Javascript, simply POST them to the server, and have the server spit out the relevant information in a hidden field...

<input type="hidden" value="id=2,foo=bar" disabled="disabled" />

Simple as that.

Note: Disable the hidden field to exclude it from the subseqient POST, if there is one ;)

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
  • Unfortunately the page to which I am redirecting is not guaranteed to be a server page. It might as well be a simple .html page with javascript that expects query string parameter and it wouldn't know how to deal with post parameters... but at the same time it can be a server page that uses post parameters... my page needs to work for both POST and GET parameters... – Victor F Nov 01 '10 at 21:17
  • 1
    check out the headers sent to gmail sometime - there is no problem sending post with query strings. – Yehosef Nov 01 '10 at 21:24
  • @Josh - There is! Like ASP.NET Web Forms, when calling an event in the server, the URL must be retained and at the same time viewstate should be sent. Not recommended by w3 though. – kazinix Jul 15 '11 at 01:21
1

The problem i think that the problem could be that postData does not contain the id parameter as it is supplied through querystring.

Posted data is in the body of the request and querystring data is in the url.

You probably need to fetch the id from request.querystring to you postdata variable.

David Mårtensson
  • 7,550
  • 4
  • 31
  • 47
  • I believe you've misunderstood my problem. It is true that postData does not contain the id parameter. But it doesn't need to contain the id since id parameters is only going to be used by javascript of the page to which I am redirecting. Unfortunately javascript can retrieve data only from GET parameters, therefore id has to be a GET parameter. But the postData has to be POST parameter, since it might not fit into GET parameter. – Victor F Nov 01 '10 at 19:24
1

Given the extra information to your question, that you require to submit to an external source, what I believe you must do is process all of the data and return a form with hidden fields. Add some javascript to submit that form to the external URL immediately upon load. Note that you won't get file uploads this way, but you can appropriately handle POST and GET data.

Brad
  • 159,648
  • 54
  • 349
  • 530
0

As far as I know, it isn't possible to redirect with POST. Couldn't you simply pretend (internally handle as if) that the request was made to the page you want to redirect the user to?

thejh
  • 44,854
  • 16
  • 96
  • 107
0

The closest answer I've found to this problem is here, but it's not transparent to the user, therefore it's not good enough for me --> Response.Redirect with POST instead of Get?

If anybody has any other suggestions, please respond!

Community
  • 1
  • 1
Victor F
  • 917
  • 16
  • 30
0

Try sending all of the data including the id in POST. Then when you are processing the data in C#, you can read the id variable in and write it back out to your webpage within a tag:

<script type="text/javascript">
  id = <%=request_id%> 
</script>

Then just make sure your javascript starts running after fully loaded with an onload() call and you're good to go.

Wade Tandy
  • 4,026
  • 3
  • 23
  • 31
0

What you are actually trying to do is redirect your POST data. May I ask why? I can't see any reason why you would want to do this if in fact both pages are on your servers.

What you should be doing is processing all of your POST data in script #1, and then redirecting to something like script2.aspx?id=234 where the ID 234 is in reference to the data in your database. You can then recall it later on script2 and dump all the data into Javascript variables for your client-side stuff to use.

Either way, something about this process sounds fishy to me. Mixing up your data processing client-side and server side is like mixing vodka and milk. It rarely works well. (But white russians sure are tasty!)

Brad
  • 159,648
  • 54
  • 349
  • 530
  • In most cases you are going to be correct, but I am writing a routing page, which adds some custom information to query string and then routes all of the data, old and new further to some URL that was provided. This page should be able to redirect to our server as well as to someone's server and it doesn't need to know where it came from or where it goes, it just simply needs to keep the same POST, GET and HEADER information as well as additional information received at this step. – Victor F Nov 01 '10 at 21:12
  • Add this detail to your question... submitting a new answer now. – Brad Nov 01 '10 at 21:13
0

Actually I was able to achieve the desired result by mixing Javascript and Codebehind code. So, what I've done is in server side code I've built an entire web page like following:

    var strForm = new StringBuilder();
    strForm.Append("<form id=\"" + formId + "\" name=\"" + formId + "\" action=\"" + url + queryString +"\" method=\"POST\">");
    foreach (string key in data)
    {
        strForm.Append("<input type=\"hidden\" name=\"" + key + "\" value=\"" + data[key].Replace("\"", "&quot;") + "\">");
    }
    strForm.Append("</form>");

And in addition to this form built on server side, I add a javascript code that submits the form I've just built.

    var strScript = new StringBuilder();
    strScript.Append("<script language='javascript'>");
    strScript.Append("var v" + formId + " = document." + formId + ";");
    strScript.Append("v" + formId + ".submit();");
    strScript.Append("</script>");
    strForm.Append("</form>");

So, what this code does, is as you see, the form action is an URL with query string parameters attached to it... but since the form method is POST, we submit the values we added as a hidden fields as POST parameters... So we end up submitting both POST and GET parameters.

Hope this solution will help somebody =)

Victor F
  • 917
  • 16
  • 30