需要帮助MVC工作流程 - 发布到另一台服务器?

Kei*_*h G 3 redirect asp.net-mvc-3

我有一个表格,我正在收集电子商务应用程序的联系信息(姓名,地址等).当用户单击"购买"按钮时,我想解析表单,获取几个值,并生成加密指纹.

然后我想从发布的表单中获取所有表单值(名称,地址等),并将其重定向到具有相同表单值的新服务器.我可能需要在幕后注入一些新的.

点击后捕获信息没问题.我只是在我的控制器上使用Buy操作.我无法弄清楚的部分是使用所需参数发布到其他服务器.

[HttpPost]
public ActionResult Buy(BuyModel model)
{
    var fingerprint = GenerateFingerprint(.....);

    return Redirect("https://some.other.url.com/");
}
Run Code Online (Sandbox Code Playgroud)

编辑:澄清.我不需要发布数据,实际上我需要在浏览器中显示响应.

Jam*_*xon 5

您可以使用HttpWebRequest类将帖子发送到其他服务器,然后照常重定向.

var httpRequest = (HttpWebRequest)WebRequest.Create("http://example.com/mypage/;
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
Run Code Online (Sandbox Code Playgroud)

对于发布数据,您需要将其转换为a byte[]然后将其添加到a stream.

string postData = "key=value&key2=value2";
byte[] dataArray = Encoding.UTF8.GetBytes(postData);
Run Code Online (Sandbox Code Playgroud)

然后,您就可以正确设置请求的ContentLength:

httpRequest.ContentLength = dataArray.Length;
Run Code Online (Sandbox Code Playgroud)

写这个Stream,我们很高兴做这个帖子:

 using(Stream requestStream = httpRequest.GetRequestStream())
{
    requestStream.Write(dataArray, 0, dataArray.Length);
    var webResponse = (HttpWebResponse)httpRequest.GetResponse();
}
Run Code Online (Sandbox Code Playgroud)

假设您不需要将用户重定向到已发布的页面,这将很有效.webResponse如果对您有用,您可以查看该对象以查找帖子中发生的情况.