ASP.NET MVC3和amp; amp;的入门Google Checkout:参加2 [英] Getting Started With ASP.NET MVC3 & Google Checkout: Take 2

查看:86
本文介绍了ASP.NET MVC3和amp; amp;的入门Google Checkout:参加2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is a follow-up to: https://stackoverflow.com/questions/6285578/getting-started-with-asp-net-mvc3-google-checkout

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

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();

首先,我遇到401错误,但问题得到解决.现在,在显示WebResponse response = request.GetResponse();的行上,我一直得到The remote server returned an error: (400) Bad Request..所以我猜我的C#代码一定有问题吗?

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?

注意:HTTP帖子应具有以下标头.

NOTE: The HTTP post should have the following headers.

授权:基本 MTIzNDU2Nzg5MDpIc1lYRm9aZkhBcXlMY0NSWWVIOHFR (这是Merchant_ID:Merchant_Key

Authorization: Basic MTIzNDU2Nzg5MDpIc1lYRm9aZkhBcXlMY0NSWWVIOHFR (which is the base64 encoding of Merchant_ID:Merchant_Key

内容类型: application/xml; charset = UTF-8

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

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

Accept: application/xml;charset=UTF-8

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

So any suggestion on how I could resolve this issue?

更新:我想我已经找到了问题的根源,但是我不知道如何解决.这是一个解释它的链接:此流不支持Seek操作

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

更新2:我终于找到了提琴手来接听电话,这是我发现的内容:

UPDATE 2: I finally got fiddler to catch the call, and here's what I found out:

请求:

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

推荐答案

您声明:

HTTP帖子应具有以下标头.

The HTTP post should have the following headers.

Content-Type:应用程序/xml; charset = UTF-8

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

显然, 在您的有效负载中不是xml,并且在跟踪中也不是xml标头...在我看来,您只是没有将正确的数据发送到API.

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.

这篇关于ASP.NET MVC3和amp; amp;的入门Google Checkout:参加2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆