How to post data and redirect to an external page?

18,097

I think I figured out how to resolve this issue. Here is what I have done.

Create an intermediate page, e.g. 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>

Then in the code behind,

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.";
    }
}

On Submit.aspx, where the button click event happens,

//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]))));
}

So effectively, we could still use Response.Redirect() to pass the data to the external link although we will need to go through a second page. In short, Submit.aspx [GET] => DoPost.aspx [Post] => The Gateway

Share:
18,097
woodykiddy
Author by

woodykiddy

Updated on June 05, 2022

Comments

  • woodykiddy
    woodykiddy almost 2 years

    I am having trouble with posting data and redirecting to an external URL. The external URL is an online payment gateway that only accepts submitted form using POST method.

    A simple html form like below will work without any issues

    <html>
    <body>
    <form action="externalpage.url" method="post">
    <input type="hidden" name="name1" value="1234">
    <input type="hidden" name="name2" value="abcd">
    <input type="submit" value="Submit Form">
    </form>
    </body>
    </html>
    

    But in my scenario, when the user clicks the button on my aspx page, I need to do some server side processing first, e.g. create a NameValueCollection object, before redirecting her to the payment gateway.

    I have tried to use the example from this link: http://www.codeproject.com/Articles/37539/Redirect-and-POST-in-ASP-NET, but the page doesn't redirect to the external URL for some reason. The following line of code doesn't seem to do anything.

    page.Controls.Add(new LiteralControl(strForm));
    
  • woodykiddy
    woodykiddy over 10 years
    Since Response.Redirect uses GET, any submitted data using GET will be rejected by the gateway unfortunately :(