入门ASP.NET MVC3&安培;谷歌结帐:取2 [英] Getting Started With ASP.NET MVC3 & Google Checkout: Take 2

查看:157
本文介绍了入门ASP.NET MVC3&安培;谷歌结帐:取2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续:<一href=\"http://stackoverflow.com/questions/6285578/getting-started-with-asp-net-mvc3-google-checkout\">Getting入门ASP.NET MVC3&安培;谷歌结帐

现在我终于开始知道发生了什么事情与谷歌Checkout的API。我决定做在服务器端的一切。所以我写了一些code,但我不能让该API成功调用。这里是我的code:

 无功海峡=的String.Format({0}:{1},MERCHANT_ID,MERCHANT_KEY);
        VAR AUTH = EN codeTo64(STR);
        VAR请求= WebRequest.Create(\"https://sandbox.google.com/checkout/api/checkout/v2/requestForm/Merchant/747839340759259\");
        ((HttpWebRequest的)要求)。接受=application / xml进行;字符集= UTF-8;
        request.Headers.Add(授权,基本+ AUTH);
        request.ContentType =application / xml进行;字符集= UTF-8;
        request.Method =POST;
        字符串POSTDATA =_type =你好;
        字节[]的字节数组= Encoding.UTF8.GetBytes(POSTDATA);
        request.ContentLength = byteArray.Length;
        流数据流= request.GetRequestStream();
        dataStream.Write(字节数组,0,byteArray.Length);
        dataStream.Close();
        //得到响应。
        WebResponse的响应= request.GetResponse();
        ViewData.Add(地位,((HttpWebResponse)响应).StatusDescription);
        数据流= response.GetResponseStream();
        VAR读者=新的StreamReader(数据流);
        字符串responseFromServer = reader.ReadToEnd();
        ViewData.Add(responseFromServer,responseFromServer);
        reader.Close();
        dataStream.Close();
        response.Close();
        返回查看();

首先,我得到一个401错误,但我得到了解决。现在,我不断收到远程服务器返回错误:上写着 WebResponse的响应= request.GetResponse()行(400)错误的请求; 。所以它是坏了我的C#code我猜?

注意:HTTP岗位应具备以下标题


  

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


  
  

内容类型:
  应用程序/ XML的,字符集= UTF-8


  
  

接受:应用程序/ XML的,字符集= UTF-8


因此​​,对我怎么能解决这个问题的任何建议?

更新:我想我找到了问题的根源,但我无法弄清楚如何解决它。下面是解释它的链接:该流不支持搜索操作

更新2:我终于小提琴手赶通话,这里是我发现了什么:

请求:

  POSThttps://sandbox.google.com/checkout/api/checkout/v2/requestForm/Merchant/747839340759259HTTP / 1.1接受:应用程序/ XML的,字符集= UTF-8内容类型:应用程序/ x-WWW的形式urlen codeD范围:字节= 1024授权:基本NzQ3ODM5MzQwNzU5MjU5OjVKNS1tRkpIZVBWc25hXzVFOW5mZ2c =用户代理:Mozilla的/ 4.0(兼容; MSIE 6.0; Windows NT的5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)主持人:sandbox.google.com内容长度:257期望:100-继续连接:保持活动
_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错误的请求内容类型:应用程序/ x-WWW的形式urlen codeD;字符集= US-ASCII传输编码:分块日期:星期四,2011年6月9日19时32分49秒GMT到期日:星期四,2011年6月9日19时32分49秒GMT缓存控制:私人,最大年龄= 0的X内容类型选项:nosniffX框选项:SAMEORIGIN的X XSS-保护:1;模式=块设置Cookie:S = payments_api = GWZzws2nBZR,KMGHgKJlTQ;过期=星期四,09军2011年20点02分49秒格林尼治标准​​时间;路径= /;安全;仅Http服务器:GSE74
_type=error&error-message=Carts+must+contain+at+least+one+item.&serial-number=c8677c3d-3e80-48e8-bd84-f01fa3b021650


解决方案

您说明:


  

相应的HTTP POST应具有以下标头。


  
  

的Content-Type:application / xml进行;字符集= UTF-8


然而,这是的明确的不是在你的XML有效载荷,这是不是在跟踪XML头......在我看来只是你没有发出正确的数据到API。

This is a follow-up to: 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

解决方案

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.

这篇关于入门ASP.NET MVC3&安培;谷歌结帐:取2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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