做到这一点需要理解HTTP重定向是如何工作的。当您使用response . redirect()时,您将发送一个带有HTTP状态码302的响应(到发出请求的浏览器),该响应告诉浏览器下一步要去哪里。根据定义,浏览器将通过GET请求来实现,即使原始请求是POST。
另一种选择是使用HTTP状态码307,它指定浏览器应该以与原始请求相同的方式发出重定向请求,但会提示用户安全警告。要做到这一点,你可以这样写:
public void PageLoad(object sender, EventArgs e)
{
// Process the post on your side
Response.Status = "307 Temporary Redirect";
Response.AddHeader("Location", "http://example.com/page/to/post.to");
}
不幸的是,这并不总是有效的。不同的浏览器实现方式不同,因为它不是一个常见的状态代码。
唉,不像Opera和FireFox开发者,IE开发者从来没有读过规范,即使是最新的、最安全的IE7也会将POST请求从域A重定向到域B,没有任何警告或确认对话框!Safari还以一种有趣的方式进行操作,虽然它不引发确认对话框并执行重定向,但它丢弃了POST数据,有效地将307重定向更改为更常见的302重定向。
所以,据我所知,实现这样的东西的唯一方法是使用Javascript。我能想到两种选择:
Create the form and have its action attribute point to the third-party server. Then, add a click event to the submit button that first executes an AJAX request to your server with the data, and then allows the form to be submitted to the third-party server.
Create the form to post to your server. When the form is submitted, show the user a page that has a form in it with all of the data you want to pass on, all in hidden inputs. Just show a message like "Redirecting...". Then, add a javascript event to the page that submits the form to the third-party server.
Of the two, I would choose the second, for two reasons. First, it is more reliable than the first because Javascript is not required for it to work; for those who don't have it enabled, you can always make the submit button for the hidden form visible, and instruct them to press it if it takes more than 5 seconds. Second, you can decide what data gets transmitted to the third-party server; if you use just process the form as it goes by, you will be passing along all of the post data, which is not always what you want. Same for the 307 solution, assuming it worked for all of your users.