简体   繁体   中英

HTTP POST Redirect from Behind code c#

<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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM