使用 MVC2 ActionResult,如何重定向(发布)到另一个站点

发布于 2024-10-10 18:19:27 字数 1203 浏览 3 评论 0原文

我有一个收集数据并“发布”到另一个站点的页面。我可以将站点 url 放入表单标记的操作中,但我想在切换站点之前将信息记录在我的数据库中。到目前为止,在 ActionResult 中,我有:

    [HttpPost]
    public ActionResult MyPage(MyPageModel model)
    {
        if (ModelState.IsValid)
        {
            StoreDate(model.fld1, model.fld2)
            var encoding = new ASCIIEncoding();
            var postData = "";
            foreach (String postKey in Request.Form)
            {
                var postValue = Encode(Request.Form[postKey]);
                postData += string.Format("&{0}={1}", postKey, postValue);
            }
            var data = encoding.GetBytes(postData);

            // Prepare web request...
            var myRequest = (HttpWebRequest)WebRequest.Create("https://www.site2.com");
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;

            // Send the data.
            Stream newStream = myRequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Flush();
            newStream.Close();

有谁知道如何完成此操作并使用正确的“返回”变体将此数据发布到其他站点。

我根据下面的回复编辑了该片段。

I have a page that collects data and 'POST's to another site. I could just put he site url in the action of the form tag but I would like to record the information in my database prior to switching sites. In the ActionResult so far I have:

    [HttpPost]
    public ActionResult MyPage(MyPageModel model)
    {
        if (ModelState.IsValid)
        {
            StoreDate(model.fld1, model.fld2)
            var encoding = new ASCIIEncoding();
            var postData = "";
            foreach (String postKey in Request.Form)
            {
                var postValue = Encode(Request.Form[postKey]);
                postData += string.Format("&{0}={1}", postKey, postValue);
            }
            var data = encoding.GetBytes(postData);

            // Prepare web request...
            var myRequest = (HttpWebRequest)WebRequest.Create("https://www.site2.com");
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;

            // Send the data.
            Stream newStream = myRequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Flush();
            newStream.Close();

Does anyone know how to finish this and use the proper 'return' varient to have this post the data to the other site.

I have edited the snippet based on a response below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

另类 2024-10-17 18:19:28

POST 已经发生,因此不会有任何灵丹妙药(即简单的 ActionResult)对您有用。由于您正在服务器上处理 POST 响应,因此您需要自己重新创建对目标服务器的 POST 请求。为此,您需要利用 HttpWebRequest这个答案。从 HttpWebRequest 获取响应后,您需要将该响应传回(可能通过 ContentResult)。总而言之,这将是不平凡的,但它是可能的。

更新
根据您的代码片段,我尝试添加以下内容:

WebResponse res = myRequest.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string returnvalue = sr.ReadToEnd();
return Content(returnValue);

The POST has already happened, so there's not going to be a magic bullet (i.e. a simple ActionResult) that will work for you. Since you're handling the POST response on your server, you'll need to recreate the POST request to the target server yourself. To do that you'll need to leverage an HttpWebRequest vis a vis this answer. After getting the response back from the HttpWebRequest, you'll need to pass that response back, probably via a ContentResult. All in all, it will be non-trivial, but it is possible.

Update:
Based on your snippet, I'd try adding the following:

WebResponse res = myRequest.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string returnvalue = sr.ReadToEnd();
return Content(returnValue);
手长情犹 2024-10-17 18:19:28

另一种选择是将表单操作指向其他站点,并在提交表单之前向您的服务器执行 ajax post。这比使用 HttpWebRequest 玩中间人要容易得多。

Another option would be to point the form action at the other site and do an ajax post to your server before submitting the form. That would be much easier than playing man-in-the-middle with HttpWebRequest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文