使用 MVC2 ActionResult,如何重定向(发布)到另一个站点
我有一个收集数据并“发布”到另一个站点的页面。我可以将站点 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
POST 已经发生,因此不会有任何灵丹妙药(即简单的
ActionResult
)对您有用。由于您正在服务器上处理 POST 响应,因此您需要自己重新创建对目标服务器的 POST 请求。为此,您需要利用HttpWebRequest
和 这个答案。从HttpWebRequest
获取响应后,您需要将该响应传回(可能通过ContentResult
)。总而言之,这将是不平凡的,但它是可能的。更新:
根据您的代码片段,我尝试添加以下内容:
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 anHttpWebRequest
vis a vis this answer. After getting the response back from theHttpWebRequest
, you'll need to pass that response back, probably via aContentResult
. All in all, it will be non-trivial, but it is possible.Update:
Based on your snippet, I'd try adding the following:
另一种选择是将表单操作指向其他站点,并在提交表单之前向您的服务器执行 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.