这是以下内容: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# 代码一定有问题?
注意:HTTP 帖子应具有以下 header 。
Authorization: Basic MTIzNDU2Nzg5MDpIc1lYRm9aZkhBcXlMY0NSWWVIOHFR (which is the base64 encoding of
Merchant_ID:Merchant_Key
Content-Type: application/xml;charset=UTF-8
Accept: application/xml;charset=UTF-8
关于如何解决这个问题有什么建议吗?
更新:我想我找到了问题的根源,但我不知道如何解决它。这是解释它的链接:This Stream Does Not Support Seek Operations
更新 2: 我终于让 fiddler 接听了电话,这是我发现的:
请求:
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
最佳答案
你说:
The HTTP post should have the following headers.
Content-Type: application/xml;charset=UTF-8
但这显然不是您有效负载中的 xml,也不是跟踪中的 xml header ...在我看来,您没有将正确的数据发送到API。
关于c# - ASP.NET MVC3 和 Google Checkout 入门 : Take 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6295359/