如何重定向到另一个服务器在mvc使用post方法

How to redirect to another server in mvc using post method?

本文关键字:mvc 使用 post 方法 服务器 重定向 另一个      更新时间:2023-09-26

我想从控制器重定向到另一个域服务器。

我还必须从服务器得到一个响应。

如何在ASP中实现这一点。净MVC吗?

我们还没有实现Model部分。我找到了一个解决web表单的方法。

    You can use the HttpWebRequest class to make the post to the other server and then redirect as normal
    //code would be something like this
    var httpRequest = (HttpWebRequest)WebRequest.Create("http:your url");
    httpRequest.Method = "POST";
    httpRequest.ContentType = "application/x-www-form-urlencoded";
    string data = "key=value&key2=value2";
    byte[] dataArray = Encoding.UTF8.GetBytes(data);
    httpRequest.ContentLength = dataArray.Length;
    //actual code to call another server
     using(Stream requestStream = httpRequest.GetRequestStream())
    {
        requestStream.Write(dataArray, 0, dataArray.Length);
        var webResponse = (HttpWebResponse)httpRequest.GetResponse();
    }