如何使用POST而不是GET c#进行重定向 [英] How to Redirect with POST and not with GET c#

查看:146
本文介绍了如何使用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#进行重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆