3

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));
halfer
  • 19,824
  • 17
  • 99
  • 186
woodykiddy
  • 6,074
  • 16
  • 59
  • 100
  • online payment! is it https url? http://www.terminally-incoherent.com/blog/2008/05/05/send-a-https-post-request-with-c/ – Damith Sep 15 '13 at 09:55

4 Answers4

1

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

woodykiddy
  • 6,074
  • 16
  • 59
  • 100
0

You need to look at HttpWebRequest.

Here's another question that pretty thoroughly explains what you need to do for your problem.

Post form data using HttpWebRequest

Community
  • 1
  • 1
Michael
  • 507
  • 5
  • 20
0

Here is my solution for you, a simple and elegant solution

Redirect and Post in ASP.NET by Samer Abu Rabie

Samer Aburabie
  • 248
  • 2
  • 8
-1

Try using

Response.Redirect("http://www.microsoft.com");

Response redirect msdn

Anobik
  • 4,841
  • 2
  • 17
  • 32
  • 1
    Since Response.Redirect uses GET, any submitted data using GET will be rejected by the gateway unfortunately :( – woodykiddy Sep 15 '13 at 09:47