我想我想出了如何解决这个问题。这是我所做的。
创建一个中间页面,例如 DoPost.aspx。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Post Form</title>
<script>
function PostData() {
document.getElementById("form1").submit();
}
</script>
</head>
<body onload="PostData()">
<form id="form1" runat="server">
<div>
<asp:Literal ID="ltrPostData" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>
然后在后面的代码中,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var url = "external.url";
form1.Action = url;
ltrPostData.Text = "Create <input type='hidden' name='data name' value='data value' /> html controls from Request.QueryString() and assign the string value to the Literal control's Text property.";
}
}
在按钮单击事件发生的 Submit.aspx 上,
//assume that the NameValueCollection object has already been created and populated
var redirectUrl = string.Format("{0}?{1}", "DoPost.aspx", BuildQueryString(data));
Response.Redirect(redirectUrl, true);
public static string BuildQueryString(NameValueCollection data)
{
// create query string with all values
return string.Join("&", data.AllKeys.Select(key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(data[key]))));
}
如此有效,我们仍然可以使用 Response.Redirect() 将数据传递到外部链接,尽管我们需要通过第二个页面。简而言之,Submit.aspx [GET] => DoPost.aspx [Post] => 网关