问题
This is a follow-up to: https://stackoverflow.com/questions/6285578/getting-started-with-asp-net-mvc3-google-checkout
Now I finally started to know what's going on with the Google Checkout API. I decided to do everything on the server side. So I wrote some code but I could not make a successful call to the API. Here's my code:
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();
First I was getting a 401 error, but I got that resolved. Now I keep getting The remote server returned an error: (400) Bad Request.
on the line that says WebResponse response = request.GetResponse();
. So it has to be something wrong with my C# code I guess?
NOTE: The HTTP post should have the following headers.
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
So any suggestion on how I could resolve this issue?
UPDATE: I think I figured out the source of the problem, but I cannot figure out how to solve it. Here's a link that explains it: This Stream Does Not Support Seek Operations
UPDATE 2: I finally got fiddler to catch the call, and here's what I found out:
REQUEST:
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
RESPONSE:
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
回答1:
You state:
The HTTP post should have the following headers.
Content-Type: application/xml;charset=UTF-8
yet that is clearly not xml in your payload, and that isn't an xml header in the trace... it looks to me simply that you aren't sending the right data to the API.
回答2:
This may be a dumb question, but is there a reason that you're not using Google's .NET DLL?
I realize the example on google code is for a windows form app, but the objects used to post the checkout request should work just fine from your controller.
来源:https://stackoverflow.com/questions/6295359/getting-started-with-asp-net-mvc3-google-checkout-take-2