Vimvq1987 Vimvq1987 - 1 year ago 69
HTML Question

How to post HTML form from server side?

I want to post this form from server side (ASP.NET/C#):

<FORM action="" method=POST>
<input type="hidden" name="merchant" value="12345678">
<input type="hidden" name="transact" value="11111111">
<input type="hidden" name="amount" value="2000">
<input type="hidden" name="currency" value="208">
<input type="hidden" name="orderid" value="11223344">
<input type="hidden" name="md5key" value="cfcd208495d565ef66e7dff9f98764da">
<input type="hidden" name="textreply" value="yes">

I'm trying to use WebClient.UploadValues. My code is

private void PostRefundRequest(Mediachase.Commerce.Orders.Payment payment)
WebClient webClient = new WebClient();
NameValueCollection refundRequest = new NameValueCollection();
PaymentMethodDto dibs = PaymentManager.GetPaymentMethodBySystemName("DIBS", SiteContext.Current.LanguageName);
string merchant = GetParameterByName(dibs, DIBSPaymentGateway.UserParameter).Value;
PurchaseOrder po = payment.Parent.Parent as PurchaseOrder;
string orderid = po.TrackingNumber;
string transact = payment.TransactionID;
string amount = (payment.Amount * 100).ToString();
refundRequest.Add("merchant", merchant);
refundRequest.Add("transact", transact);
refundRequest.Add("amount", amount );

refundRequest.Add("currency", payment.Parent.Parent.BillingCurrency);
refundRequest.Add("orderid", orderid);
string md5 = GetMD5KeyRefund(merchant, orderid, transact, amount);
refundRequest.Add("md5key", md5);
refundRequest.Add("textreply", "yes");
byte[] responseArray = webClient.UploadValues("https://<mylogin>:<mypassword>", refundRequest);

I tried to debug, and everything seems to be correct, but the API returns HTTP 401 (Unauthorized). My concern is: does WebClient.UploadValues support the url of the API (which includes username/password)

Thank you.

Answer Source

You got to have Post in method parameter for UploadValues :

NameValueCollection data = new NameValueCollection();
data["input-data1"] = "value1";
data["input-data2"] = "value2";
data["input-data3"] = "value3";

WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential(mylogin, mypassword);
byte[] responseBytes = webClient.UploadValues("", "POST", data);
string response = Encoding.UTF8.GetString(responseBytes);

Update: Maybe this is not enough, because in documentation is stated that POST is default for http scheme ( I presume that is the same for https

Update 2: Microsoft disabled that authentication in URL for internet explorer ( for security reasons, so probably that is disabled for web client to