1
<form action="https://demo.bank.com/payment" method="post">
<input name="x_login" type="hidden" runat="server" id="x_login" />
<input name="x_amount" type="hidden" runat="server" id="x_amount" />
<input name="x_fp_sequence" type="hidden" runat="server" id="x_fp_sequence" />
<input name="x_fp_timestamp" type="hidden" runat="server" id="x_fp_timestamp" />
<input name="x_fp_hash" type="hidden" runat="server" id="x_fp_hash" />
<input name="x_show_form" type="hidden" runat="server" id="x_show_form" />
<input name="x_line_item" value="<%# Eval("Desc") %>" type="hidden" />
<input type="submit" name="submit" value="Submit" class="btn_confirm" />
</form>

Above is a sample code that I use to POST data onto a bank website for credit card processing. It work fine but I need to do this behind code instead. I've try the WebClient() and HttpClient(), both of them only past the data and don't redirect the browser(unless I not coding it correctly). The don't seem to redirect the user to the bank website. Is this even possible? If so, how can I accomplish this? PS:Sorry, I'm new to pasting POST data to external URL.

zAnthony
  • 332
  • 4
  • 17
  • Take a look at this https://stackoverflow.com/questions/4015324/http-request-with-post – Bradley Uffner Jul 13 '16 at 14:30
  • @itsme86 What he has there is pretty common test code when submitting data to a bank. He needs to convert it to do a POST programatically ,without a UI. – Bradley Uffner Jul 13 '16 at 14:32
  • That is correct. It is a test code. I need to do it programmatically without a UI. – zAnthony Jul 13 '16 at 14:41
  • The first comment has a link to a bunch of examples of how to do it programatically, everything you need should be in there. – Bradley Uffner Jul 13 '16 at 14:45
  • I did implement both HttpClient() as well as WebClient() and none of them redirect me to the POST URL. I need the client browser to go to the POST URL because it will parse the values and display it in a checkout page which allow them to enter their credit card information. Appreciate if you can point out what I might be doing wrong. – zAnthony Jul 13 '16 at 15:04
  • Ah, sorry, I misunderstood your question. You will probably need to do this client side, in Java script. Unfortunately that isn't my area of expertise. – Bradley Uffner Jul 13 '16 at 15:07
  • "None of them redirect me to the POST URL", I don't understand what you mean. This is something about the protocol itself, you need to understand what you do in a regular POST before translating this to code. WebClient is just a HTTP wrapper, I don't like it but it works, you upload your values there. I will write an example – Maximiliano Rios Jul 13 '16 at 15:10

1 Answers1

0

You have to open the connection and POST your values through the WebClient. Remember webclient is just a wrapper. For the sake of simplicity I've only added two parameters, an empty one (x_login) and one with a value. You have to include the key in order to be serialized and posted.

<input name="x_login" type="hidden" runat="server" id="x_login" />
<input name="x_line_item**" value="<%# Eval("Desc") %>" type="hidden" />
using(WebClient client = new WebClient())
{
    var reqparm = new System.Collections.Specialized.NameValueCollection();
    reqparm.Add("x_login", ""); // You're sending the key with en empty value
    reqparm.Add("x_line_item", Eval("Desc") );
    byte[] responsebytes = client.UploadValues("https://demo.bank.com/payment", "POST", reqparm);
    string responsebody = Encoding.UTF8.GetString(responsebytes);
}
Maximiliano Rios
  • 2,196
  • 2
  • 20
  • 34
  • Quick question, would I be able to add the same string name twice. I need to loop through a list and issue more than one string value for x_line_item. Would that be possible? Can I create a foreach loop to add multiple x_line_item. The bank require multiple line item string key and not ONE. reqparm.Add("x_line_item", Eval("Desc") ); – zAnthony Jul 13 '16 at 16:07
  • Of course, there's no limitation on that. In fact you will be posting an array of elements which is something very normal. Depending on how they're are processed there could be some limitations but I think it will work if you've already received requirements of adding N elements of the same type. – Maximiliano Rios Jul 13 '16 at 16:35
  • I tried it, I can't add more than one x_line_item into the NameValueCollection. It see that x_line_item already exist and append the new key value by comma delimited. – zAnthony Jul 13 '16 at 16:44
  • That's the way NameValueCollection handles that. Try it yourself adding two or more values and you will see the browser translates that to the exact same csv list – Maximiliano Rios Jul 13 '16 at 16:58
  • I'm confuse now. I did a lot of reading and I can't seem to wire it up where it past the POST data the the ACTION URL and at the same time take me to that ACTION URL. I can only post back the response unto my own webpage. The way I need it to work, is post some order detail using POST verb via a button and then it will redirect the user to the POST page where they can enter the credit card make the payment. – zAnthony Jul 13 '16 at 20:10
  • Technically you never GO there. I suggest you to understand how HTTP works to understand what to do. You always request something from a server through a VERB /url and protocol and receive an answer sending or not information. "Going" to URLs doesn't exist, it's highly probable that inside of the website that takes your POST there's a redirection (304 or 307 for example) that tell you to go somewhere else. But it the service itself does it automatically you cannot do anything. – Maximiliano Rios Jul 13 '16 at 20:34
  • For example, let's imagine I need certain information from an entity, I will do: GET /entity/1. I receive a 200 OK and some information. Now, I need to POST data related to this, I execute POST /entity/1/event to (let's say) inform an event and some information in the body. There's no "going" anywhere. But in this post I'm informed of the location of the object created or a next step and I receive a header with the next information. That's all you can do, if the server redirects you internally it's not a service you can "consume" programatically – Maximiliano Rios Jul 13 '16 at 20:35
  • I appreciate you taking the time to help me. I actually understand what you're saying because in my MVC applications, I redirect the users browser once my BLL save the data into my db. Looks like I have some more research to do. Thank you for the help. – zAnthony Jul 13 '16 at 20:48
  • You're welcome, it's a matter of understanding the protocol to see what you want to do. Feel free to ask me any question you need to get some help to research this. – Maximiliano Rios Jul 13 '16 at 21:23