如何使用post方法重定向到mvc中的另一台服务器?

时间:2015-05-29 08:25:18

标签: javascript c# jquery asp.net-mvc asp.net-ajax

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

此外,我必须得到服务器的回复。

如何在ASP.NET MVC中实现这一点?

我们尚未实施“模型”部分。我找到了一个Web表单的解决方案。

1 个答案:

答案 0 :(得分:0)

    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();
    }