同时使用 POST 和 GET 的 HttpWebRequest
我需要使用以下命令将用户重定向到 http://www.someurl.com?id=2一个 POST 方法。 可能吗?如果可以,那么如何实现?
现在我有以下内容,它正确转发 POST 数据,但它删除了 ?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());
}
我需要查询字符串数据的原因 -> ?id=2 和 POST 数据是因为我将查询字符串传递到页面,其中 javascript 将处理查询字符串数据,而 .NET 将处理通过 POST 方法发送的数据。我传递的 POST 数据可能比 GET 方法允许的最大字符数长,因此我不能仅使用 GET 方法...那么,您有什么建议?
更多信息: 我正在编写一个路由页面,它将一些自定义信息添加到查询字符串,然后将所有旧数据和新数据进一步路由到提供的某个 URL。该页面应该能够重定向到我们的服务器以及某人的服务器,并且不需要知道它来自哪里或去哪里,它只需要保留相同的 POST、GET 和 HEADER 信息以及在此步骤收到的附加信息。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不。没有任何理由混合使用 POST 和 GET。
如果您需要使请求中传入的参数可用于 Javascript,只需将它们 POST 到服务器,然后让服务器在隐藏字段中吐出相关信息......
就这么简单。
注意:禁用隐藏字段以将其从后续 POST 中排除(如果有的话);)
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...
Simple as that.
Note: Disable the hidden field to exclude it from the subseqient POST, if there is one ;)
我认为问题可能是 postData 不包含 id 参数,因为它是通过查询字符串提供的。
发布的数据位于请求正文中,查询字符串数据位于 url 中。
您可能需要将 id 从 request.querystring 获取到 postdata 变量。
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.
鉴于您的问题需要提交给外部来源的额外信息,我相信您必须做的是处理所有数据并返回带有隐藏字段的表单。添加一些 JavaScript 以在加载后立即将该表单提交到外部 URL。请注意,您不会以这种方式获得文件上传,但您可以适当地处理 POST 和 GET 数据。
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.
据我所知,不可能使用 POST 进行重定向。难道您不能简单地假装(在内部处理为好像)该请求是向您要将用户重定向到的页面发出的吗?
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?
我发现这个问题最接近的答案就在这里,但它对用户来说并不透明,因此对我来说还不够好 --> Response.Redirect 使用 POST 而不是 Get?
如果有人还有其他建议,请回复!
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!
尝试在 POST 中发送所有数据,包括 id。然后,当您在 C# 中处理数据时,您可以读取 id 变量并将其写回标签内的网页:
然后只需确保您的 JavaScript 在通过 onload() 调用完全加载后开始运行,并且您可以很好,可以走了。
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:
Then just make sure your javascript starts running after fully loaded with an onload() call and you're good to go.
您实际上想做的是重定向您的 POST 数据。请问为什么?如果事实上这两个页面都在您的服务器上,我看不出您有任何理由要这样做。
您应该做的是处理脚本 #1 中的所有 POST 数据,然后重定向到类似 script2.aspx?id=234 的内容,其中 ID 234 引用数据库中的数据。然后,您可以稍后在 script2 上调用它,并将所有数据转储到 Javascript 变量中,供客户端使用。
不管怎样,这个过程对我来说有些可疑。混合数据处理客户端和服务器端就像混合伏特加和牛奶。它很少能发挥良好的作用。 (但是俄罗斯白种人确实很好吃!)
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!)
实际上,我能够通过混合 Javascript 和代码隐藏代码来达到预期的结果。因此,我所做的是在服务器端代码中构建了一个完整的网页,如下所示:
除了在服务器端构建的表单之外,我还添加了一个 javascript 代码来提交我刚刚构建的表单。
因此,这段代码的作用是,如您所见,表单操作是一个附加有查询字符串参数的 URL...但由于表单方法是 POST,因此我们将作为隐藏字段添加的值作为 POST 参数提交。 .. 所以我们最终提交了 POST 和 GET 参数。
希望这个解决方案能帮助别人=)
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:
And in addition to this form built on server side, I add a javascript code that submits the form I've just built.
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 =)