HttpWebRequest with POST and GET at the same time

谁都会走 提交于 2020-01-04 05:23:41

问题


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.


回答1:


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 ;)




回答2:


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.




回答3:


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.




回答4:


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?




回答5:


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!




回答6:


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.




回答7:


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!)




回答8:


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 =)



来源:https://stackoverflow.com/questions/4071988/httpwebrequest-with-post-and-get-at-the-same-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!