string xml = "<?xml version='1.0'? encoding='UTF-8'?><!DOCTYPE paymentService PUBLIC '-//WorldPay//DTD WorldPay PaymentService v1//EN''http://dtd.worldpay.com/paymentService_v1.dtd'><paymentService version='1.4' merchantCode='MYMERCHANTCODE'><submit><order orderCode='RecurringOrderCode'><description>Monthly subscription.</description><amount value='1399' currencyCode='EUR' exponent='2' /><orderContent>Your Original Order Content</orderContent> <paymentDetails><VISA-SSL> <cardNumber>4444333322221111</cardNumber><expiryDate> <date month='09' year='2019'/> </expiryDate> <cardHolderName>J. Shopper</cardHolderName><cvc>123</cvc> <cardAddress> <address> <street>47A Queensbridge Rd</street><postalCode>CB94BQ</postalCode><city>GB</city><countryCode>GB</countryCode><telephoneNumber>+44</telephoneNumber> </address> </cardAddress> </VISA-SSL> <session shopperIPAddress='100.100.100.100' id='0215ui8ib1' /> </paymentDetails></order></submit></paymentService>";
string url = "https://secure-test.worldpay.com/jsp/merchant/xml/paymentService.jsp";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
//string s = "id="+Server.UrlEncode(xml);
byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(xml);
req.Method = "POST";
req.ContentType = "text/xml;charset=utf-8";
req.ContentLength = requestBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
string backstr = sr.ReadToEnd();
sr.Close();
res.Close();
return View();
我正在使用此代码集成Worldpay支付网关API我收到此错误以获取来自HttpWebResponse行的响应res =(HttpWebResponse)req.GetResponse();
错误是-401需要授权
答案 0 :(得分:1)
您需要将凭据添加到请求中,可能是HTTP基本身份验证。可以这样做:
req.Headers[HttpRequestHeader.Authorization] = "Basic " +
Convert.ToBase64String(Encoding.ASCII.GetBytes(<username> + ":" + <password>));