如何使用POST而不是GET c#进行重定向

编程入门 行业动态 更新时间:2024-10-25 03:31:21
本文介绍了如何使用POST而不是GET c#进行重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用带有GET方法的重定向,例如这样(工作正常):

I am using Redirect with GET method like this (works fine):

Response.Redirect(string.Format("../NewPage.aspx?Name1 = {0}& Name2 = {1}","name1","name2");

但是我想使用POST方法,因此客户端无法访问这些变量.我搜索发现" Response.使用POST而不是Get进行重定向?"

But I would like to use POST method, so the client has no access to these variables. I searched and found "Response.Redirect with POST instead of Get?"

目前,我有以下描述的代码:

Currently I have the following piece of code as it is described:

StringBuilder postData = new StringBuilder(); postData.Append("Name1=" + HttpUtility.UrlEncode("Name1") + "&"); postData.Append("Name2=" + HttpUtility.UrlEncode("Name2")); //ETC for all Form Elements // Now to Send Data. StreamWriter writer = null; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = postData.ToString().Length; try { writer = new StreamWriter(request.GetRequestStream()); writer.Write(postData.ToString()); } finally { if (writer != null) writer.Close(); } Response.Redirect("~/NewPage.aspx");

我的问题是,如何在NewPage.aspx页面中使用/获取传递的变量?

My question is, how can I use/get the passed variables in NewPage.aspx page?

感谢您的帮助.

推荐答案

实际上,您可以使用HTTP状态码307进行POST重定向;有时存在有效的用例,例如在银行应用程序中.

You can actually do a POST redirect, by using HTTP status code 307; and sometimes there are valid use cases, for example in a banking application.

"307临时重定向(自HTTP/1.1开始):在这种情况下,应使用另一个URI重复该请求;但是,以后的请求仍应使用原始URI.与历史上实现302的方式相反,重新发出原始请求时,不允许更改request方法.例如,应使用另一个POST请求重复POST请求."(源)

在此处了解更多信息.

当然,这是否是您的良好做法,这是另一个问题.可能还有其他与保持状态相关的变通办法,以避免篡改.

Of course, whether this is good practice in your case, is another question; there might be other workarounds related to keeping state around to avoid tampering.

刚刚注意到,在此处a>.

更多推荐

如何使用POST而不是GET c#进行重定向

本文发布于:2023-10-10 22:35:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479943.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   而不是   重定向   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!