ASP.NET MVC3和Google Checkout入门:拿2

这是对以下内容的跟进: https : //stackoverflow.com/questions/6285578/getting-started-with-asp-net-mvc3-google-checkout

现在我终于开始了解Google Checkout API的用途。 我决定在服务器端做所有事情。 所以我写了一些代码,但我无法成功调用API。 这是我的代码:

var str = string.Format("{0}:{1}", MERCHANT_ID, MERCHANT_KEY); var auth = EncodeTo64(str); var request = WebRequest.Create("https://sandbox.google.com/checkout/api/checkout/v2/requestForm/Merchant/747839340759259"); ((HttpWebRequest) request).Accept = "application/xml;charset=UTF-8"; request.Headers.Add("Authorization", "Basic " + auth); request.ContentType = "application/xml;charset=UTF-8"; request.Method = "POST"; string postData = "_type=hello"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); // Get the response. WebResponse response = request.GetResponse(); ViewData.Add("status", ((HttpWebResponse)response).StatusDescription); dataStream = response.GetResponseStream(); var reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); ViewData.Add("responseFromServer", responseFromServer); reader.Close(); dataStream.Close(); response.Close(); return View(); 

首先我得到401错误,但我解决了。 现在我不断收到The remote server returned an error: (400) Bad Request. 在说明WebResponse response = request.GetResponse(); 。 所以我想我的C#代码有问题吗?

注意:HTTPpost应具有以下标头。

授权:基本MTIzNDU2Nzg5MDpIc1lYRm9aZkhBcXlMY0NSWWVIOHFR (这是Merchant_ID:Merchant_Key的base64编码Merchant_ID:Merchant_Key

Content-Type:application / xml; charset = UTF-8

接受:application / xml; charset = UTF-8

那么关于如何解决这个问题的任何建议?

更新:我想我找出了问题的根源,但我无法弄清楚如何解决它。 这是一个解释它的链接: 此流不支持寻求操作

更新2:我终于得到了小提琴手接听电话,这就是我发现的:

请求:

 POST https://sandbox.google.com/checkout/api/checkout/v2/requestForm/Merchant/747839340759259 HTTP/1.1 Accept: application/xml;charset=UTF-8 Content-Type: application/x-www-form-urlencoded Range: bytes=1024- Authorization: Basic NzQ3ODM5MzQwNzU5MjU5OjVKNS1tRkpIZVBWc25hXzVFOW5mZ2c= User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727) Host: sandbox.google.com Content-Length: 257 Expect: 100-continue Connection: Keep-Alive _type=checkout-shopping-cart&item_name_1=Baseball&item_description_1=White+baseball&item_currency_1=USD&item_price_1=5.99&item_quantity_1=2&item_name_2=Baseball+Glove&item_description_2=XL+Baseball+Glove&item_currency_2=USD&item_price_2=30&item_quantity_2=1 

响应:

 HTTP/1.1 400 Bad Request Content-Type: application/x-www-form-urlencoded; charset=US-ASCII Transfer-Encoding: chunked Date: Thu, 09 Jun 2011 19:32:49 GMT Expires: Thu, 09 Jun 2011 19:32:49 GMT Cache-Control: private, max-age=0 X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block Set-Cookie: S=payments_api=GWZzws2nBZR-KMGHgKJlTQ; Expires=Thu, 09-Jun-2011 20:02:49 GMT; Path=/; Secure; HttpOnly Server: GSE 74 _type=error&error-message=Carts+must+contain+at+least+one+item.&serial-number=c8677c3d-3e80-48e8-bd84-f01fa3b02165 0 

你说:

HTTPpost应该有以下标题。

Content-Type:application / xml; charset = UTF-8

显然不是你的有效负载中的xml,而且这不是跟踪中的xml标题……我只是觉得你没有向API发送正确的数据。

这可能是一个愚蠢的问题,但有没有理由你不使用谷歌的.NET DLL?

我意识到谷歌代码的示例是针对Windows窗体应用程序,但用于发布结帐请求的对象应该可以在您的控制器上正常工作。