我想从服务器端发布此表单(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>
我正在尝试使用WebClient.UploadValues。我的代码是
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);
}
我尝试调试,一切似乎都是正确的,但API返回HTTP 401(未经授权)。我担心的是:WebClient.UploadValues是否支持API的URL(包括用户名/密码)
谢谢。
答案 0 :(得分:3)
你必须为UploadValues提供Post in方法参数:
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);
更新:也许这还不够,因为在文档中声明POST是http scheme(http://msdn.microsoft.com/en-us/library/9w7b4fz7.aspx的默认值)我认为https是相同的
更新2:出于安全原因,Microsoft在Internet Explorer(http://support.microsoft.com/kb/834489)的URL中禁用了该身份验证,因此可能已禁用Web客户端
答案 1 :(得分:-4)
只需为表单提供ID并使用以下代码提交
$('#formid').submit();