【问题标题】:How to redirect to another server in mvc using post method?如何使用 post 方法重定向到 mvc 中的另一台服务器?
【发布时间】:2015-05-29 08:25:17
【问题描述】:

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

我还必须得到服务器的响应。

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

我们尚未实现模型部分。我找到了 Web 表单的解决方案。

【问题讨论】:

  • 你不能用 C# 代码做一个HttpClient POST 并得到响应吗?

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


【解决方案1】:
    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();
    }

【讨论】:

    猜你喜欢
    • 2018-12-15
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 2013-05-27
    相关资源
    最近更新 更多