如何执行HTTP POST和用户重定向到一个外部网站? [英] How to perform an HTTP POST and redirect the user to an external site?

查看:473
本文介绍了如何执行HTTP POST和用户重定向到一个外部网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,作为标题暗示一个人如何执行HTTP POST到外部网站,同时将用户重定向呢?我找了一些类似的:

So as the title suggest how does one perform an HTTP POST to an external website while also redirecting the user there? I am looking for something similar to this:

Redirect("MyWebsite.com", HttpAction.POST, new RedirectData(new { item = "itemValue"}));

它在ASP.NET MVC来完成。

It has to be done in ASP.NET mvc.

修改

此行​​为是必要的外部网站实现身份验证。该算法的验证:

This behaviour is necessary to implement authentication on an external website. The algorithm for the authentication:


  1. 用户导航到需要他被识别的网页。

  2. 的网页调用验证网站的网络服务,并初始化身份验证。

  3. 初始化返回GUID有被张贴到指定的URL(比方说,它是MyWebsite.com)。

  4. 用户应该被重定向到的数据被张贴到URL。

到目前为止,我设法想出这个:

So far I've managed to come up with this:

string url = "MyWebsite.com";
NameValueCollection formData = new NameValueCollection();
formData["ticket"] = Ticket.ticket;
WebClient webClient = new WebClient();
byte[] responseBytes = webClient.UploadValues(url, "POST", formData);

Response.BinaryWrite(responseBytes);
Response.Redirect("MyWebsite.com", true);

我得到的答复是该网站的HTML我应该被重定向到,问题是 - 现在怎么办?我如何告诉,显示HTML,我通过响应得到了浏览器吗?如果我只是重定向到MyWebSite.com,将执行一个GET操作,这不是我所期待的。做响应的二进制写无所作为 - 它只是写POST操作,以我目前执行操作的响应 - 没有重定向到MyWebSite.com。

The response I get is the HTML of the website I am supposed to be redirected to and the question is - what now? How do I tell the browser to display HTML that I got via the response? If I simply redirect to "MyWebSite.com" it would execute a GET action, which is not what I am looking for. Doing binary write of the response accomplishes nothing - it just writes the response of the POST action to the action I am currently executing - there is no redirect to "MyWebSite.com".

推荐答案

如果您想要发布,并重定向到一个外部网站,那么你应该让你的窗体的行动直接进入该网址。没有理由来处理在你身边任何东西。但是,除非你控制的外部站点或有某种安排,才能够做到这一点,它很可能将无法正常工作。几乎每一个平台实现了某种正是这种类型的事情CSRF保护prevent的。

If you want to post and redirect to an external site, then you should just make your form's action go to that URL directly. There's no reason to handle anything on your side. However, unless you control the external site or have some sort of arrangement to be able to do this, it very likely won't work. Just about every platform implements some sort of CSRF protection to prevent exactly this type of thing.

这篇关于如何执行HTTP POST和用户重定向到一个外部网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆