HttpWebRequest with POST and GET at the same time

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.


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


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.


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.

链接地址: http://www.djcxy.com/p/41094.html

上一篇: 用Jasmine测试服务函数POST响应

下一篇: HttpWebRequest与POST和GET在同一时间