将 xml 发布到 Google Checkout

发布于 2024-08-19 12:09:43 字数 1667 浏览 7 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

又怨 2024-08-26 12:09:43

正如 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 a Response object that might have a more detailed error message that can be read by calling its GetResponseStream() method.

丢了幸福的猪 2024-08-26 12:09:43

由于对 Google Checkout API 一无所知,您确定不需要这些元素的每个上的命名空间吗?

XElement rootElement = new XElement(ns + "checkout-shopping-cart",
    new XElement(ns + "shopping-cart"),
        new XElement(ns + "items",
                     // etc

这确实是 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?

XElement rootElement = new XElement(ns + "checkout-shopping-cart",
    new XElement(ns + "shopping-cart"),
        new XElement(ns + "items",
                     // etc

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.

表情可笑 2024-08-26 12:09:43

如果异常是 WebException,您仍然可以读取响应消息。这将为您提供有关问题所在的更多信息:

try {
   response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex1) {
   response = ex1.Response();
}

You still can read response message, if exception is WebException. This will give you more information on what's wrong:

try {
   response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex1) {
   response = ex1.Response();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文