将 xml 发布到 Google Checkout
我正在尝试使用 Google Checkout,但在发布到结账服务器时遇到问题。这是我的代码:
XNamespace ns = XNamespace.Get("http://checkout.google.com/schema/2");
XDocument cart = new XDocument();
XElement rootElement = new XElement(ns + "checkout-shopping-cart",
new XElement("shopping-cart",
new XElement("items",
new XElement("item",
new XElement("item-name", "doodad"),
new XElement("item-description", "Description for the doodad"),
new XElement("unit-price", 9.99, new XAttribute("currency", "GBP")),
new XElement("quantity", 1)
)
)
)
);
cart.Add(rootElement);
string authKey = "111222333444:NOTAREALKEY";
authKey = EncodeToBase64(authKey);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://checkout.google.com/cws/v2/Merchant/111222333444/merchantCheckout");
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(cart.ToString());
request.ContentType = "application/xml; charset=UTF-8";
request.ContentLength = byteArray.Length;
request.Headers.Add("Authorization: Basic " + authKey);
request.Accept = "application/xml; charset=UTF-8";
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Exception here!
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseText = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
当我调用 GetResponse() 时,我收到一个 (400) Bad Request
。
对此的任何帮助将不胜感激。
I'm experimenting with using Google Checkout and am having a problem posting to the checkout server. Here is my code:
XNamespace ns = XNamespace.Get("http://checkout.google.com/schema/2");
XDocument cart = new XDocument();
XElement rootElement = new XElement(ns + "checkout-shopping-cart",
new XElement("shopping-cart",
new XElement("items",
new XElement("item",
new XElement("item-name", "doodad"),
new XElement("item-description", "Description for the doodad"),
new XElement("unit-price", 9.99, new XAttribute("currency", "GBP")),
new XElement("quantity", 1)
)
)
)
);
cart.Add(rootElement);
string authKey = "111222333444:NOTAREALKEY";
authKey = EncodeToBase64(authKey);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://checkout.google.com/cws/v2/Merchant/111222333444/merchantCheckout");
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(cart.ToString());
request.ContentType = "application/xml; charset=UTF-8";
request.ContentLength = byteArray.Length;
request.Headers.Add("Authorization: Basic " + authKey);
request.Accept = "application/xml; charset=UTF-8";
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Exception here!
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseText = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
When I call GetResponse(), I get a (400) Bad Request
.
Any assistance on this would be gratefully received.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Jon Skeet 指出的那样,您的 XML 看起来已损坏:-)。为了进一步帮助调试 - 响应中可能有有关错误的更多信息。
WebException
有一个Response
对象,该对象可能包含更详细的错误消息,可以通过调用其GetResponseStream()
方法来读取该消息。Your XML looks broken as Jon Skeet points out :-). In order to further aid debugging - there may be more information about the error in the response.
WebException
has aResponse
object that might have a more detailed error message that can be read by calling itsGetResponseStream()
method.由于对 Google Checkout API 一无所知,您确定不需要这些元素的每个上的命名空间吗?
这确实是 Checkout API 指南向我建议的内容 - 注意“xmlns=...”表示这是该元素和所有后代元素的命名空间,除非另有指定。
Not knowing anything about the Google Checkout API, are you sure you don't need the namespace on each of those elements?
That's certainly what the Checkout API guide suggests to me - note that "xmlns=..." means that's the namespace for this element and all descendant elements unless otherwise specified.
如果异常是 WebException,您仍然可以读取响应消息。这将为您提供有关问题所在的更多信息:
You still can read response message, if exception is WebException. This will give you more information on what's wrong: