Vimvq1987 Vimvq1987 - 5 months ago 9
HTML Question

How to post HTML form from server side?

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

<FORM action="https://login:password@payment.architrade.com/cgi-adm/refund.cgi" 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">
</FORM>


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>@payment.architrade.com/cgi-adm/refund.cgi", 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

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("http://www.example.com/posttome.aspx", "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 (http://msdn.microsoft.com/en-us/library/9w7b4fz7.aspx) I presume that is the same for https

Update 2: Microsoft disabled that authentication in URL for internet explorer (http://support.microsoft.com/kb/834489) for security reasons, so probably that is disabled for web client to