1

I'm using webforms and I need to create and POST an http form to a payment gateway for one of my clients. I'm using a user control that is displayed in a masterpage, however they already have an existing <form> on the page, so I'm pretty sure I cannot add another.

What is the best practice method to create and post a form in C# from the code behind? Currently I have the following:

var nvc = new System.Collections.Specialized.NameValueCollection
    {
        {"EWAY_ACCESSCODE", ewayResponse.AccessCode},
        {"EWAY_CARDNAME", txtCcName.Text},
        {"EWAY_CARDNUMBER", txtCcNumber.Text},
        {"EWAY_CARDEXPIRYMONTH", ddlCcExpMonth.SelectedValue},
        {"EWAY_CARDEXPIRYYEAR", ddlCcExpYear.SelectedValue},
        {"EWAY_CARDCVN", txtCcv.Text}
    };

    var client = new WebClient();
    client.UploadValues(ewayResponse.FormActionURL, nvc);

This does work, but is there a better / alternative way to do it?

Alex
  • 1,549
  • 2
  • 16
  • 41
  • cooooooooooooooo, this might give some insight: http://stackoverflow.com/questions/5401501/how-to-post-data-to-specific-url-using-webclient-in-c-sharp – kyndigs Nov 11 '13 at 02:32

2 Answers2

4

Aside from the previous suggestion:

  • why wouldn't a (simpler) Button.PostbackUrl work?

    <asp:Button ID="externalPost" runat="server" PostBackUrl="http://www.somewhere.com/" Text="Post Somewhere" />
    

Also, assuming that's a payment gateway you're referring to, you're doing a server-side POST, which has implications on PCI Compliance (vs. a direct form post from client/browser).

Hth.

EdSF
  • 11,753
  • 6
  • 42
  • 83
  • I agree with the PCI stuff, that's why I'd like to do find a better solution. So the best option would be IrishChieftain's solution? Or is there a more elegant way to do it? – Alex Nov 11 '13 at 06:14
  • @AlexFrame As above, any reason why `Button.PostBackUrl` wouldn't work? What it will do is actually do a direct post to the url you set, resolving the PCI matter. I haven't tried the suggestion below, but a quick read of the code allows you to remove the server-side `
    ` tag. Of course, that would also mean that some other code maybe affected (e.g. other controls on page that rely on default Postback behavior), which you will then need to adjust.
    – EdSF Nov 11 '13 at 15:09
  • It "toggles" the form tag so no other controls are affected. I used it in five different E-Commerce sites I have developed. You can use JavaScript presuming it's enabled on all clients. Only other solution is to add a second pair of form tags and that's a hack that nobody has yet explained ;-) – IrishChieftain Nov 13 '13 at 15:45