使用 ASP.net 的 Google Checkout HTTP Post

发布于 2024-08-16 23:34:01 字数 307 浏览 7 评论 0原文

我有 2 个在 ASP.net(C#) 中创建的页面。第一个(称为 Shoppingcart.asp)有一个“立即购买”按钮。第二个(称为 processpay.asp)只是等待 google checkout 向其发送 HTTP 请求来处理付款。我想要做的是向 google checkout 发送一个 post 语句,其中包含几个我想要传递回 processpay.asp 的变量(即 clientid=3&itemid=10),但我不知道如何格式化 POST HTTP声明或我必须在 google checkout 中更改哪些设置才能使其正常工作。

任何想法将不胜感激。

I have 2 pages I created in ASP.net(C#). The first one(called shoppingcart.asp) has a buy it now button. The second one(called processpay.asp) just waits for google checkout to send an HTTP request to it to process the payment. What I would like to do send a post statement to google checkout with a couple of variables that I want passed back to processpay.asp(ie clientid=3&itemid=10), but I don't know how to format the POST HTTP statement or what settings I have to change in google checkout to make it work.

Any ideas would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帥小哥 2024-08-23 23:34:01

Google Checkout 提供了示例代码以及有关如何将其与任何 .NET 应用程序集成的教程:

请务必检查标题为“将示例代码集成到您的网络应用程序中"。


但是,如果您更喜欢使用服务器端 POST,则可能需要检查以下方法,该方法提交 HTTP post 并以字符串形式返回响应:

using System.Net;

string HttpPost (string parameters)
{ 
   WebRequest webRequest = WebRequest.Create("http://checkout.google.com/buttons/checkout.gif?merchant_id=1234567890");
   webRequest.ContentType = "application/x-www-form-urlencoded";
   webRequest.Method = "POST";

   byte[] bytes = Encoding.ASCII.GetBytes(parameters);

   Stream os = null;

   try
   { 
      webRequest.ContentLength = bytes.Length;
      os = webRequest.GetRequestStream();
      os.Write(bytes, 0, bytes.Length);      
   }
   catch (WebException e)
   {
      // handle e.Message
   }
   finally
   {
      if (os != null)
      {
         os.Close();
      }
   }

   try
   { 
      // get the response

      WebResponse webResponse = webRequest.GetResponse();

      if (webResponse == null) 
      { 
          return null; 
      }

      StreamReader sr = new StreamReader(webResponse.GetResponseStream());

      return sr.ReadToEnd().Trim();
   }
   catch (WebException e)
   {
      // handle e.Message
   }

   return null;
} 

参数需要以以下形式传递:name1=value1&名称2=值2

Google Checkout has sample code and a tutorial on how to integrate it with any .NET application:

Make sure to check the section titled: "Integrating the Sample Code into your Web Application".


However, if you prefer to use a server-side POST, you may want to check the following method which submits an HTTP post and returns the response as a string:

using System.Net;

string HttpPost (string parameters)
{ 
   WebRequest webRequest = WebRequest.Create("http://checkout.google.com/buttons/checkout.gif?merchant_id=1234567890");
   webRequest.ContentType = "application/x-www-form-urlencoded";
   webRequest.Method = "POST";

   byte[] bytes = Encoding.ASCII.GetBytes(parameters);

   Stream os = null;

   try
   { 
      webRequest.ContentLength = bytes.Length;
      os = webRequest.GetRequestStream();
      os.Write(bytes, 0, bytes.Length);      
   }
   catch (WebException e)
   {
      // handle e.Message
   }
   finally
   {
      if (os != null)
      {
         os.Close();
      }
   }

   try
   { 
      // get the response

      WebResponse webResponse = webRequest.GetResponse();

      if (webResponse == null) 
      { 
          return null; 
      }

      StreamReader sr = new StreamReader(webResponse.GetResponseStream());

      return sr.ReadToEnd().Trim();
   }
   catch (WebException e)
   {
      // handle e.Message
   }

   return null;
} 

Parameters need to be passed in the form: name1=value1&name2=value2

若沐 2024-08-23 23:34:01

代码最终可能看起来像这样:

GCheckout.Checkout.CheckoutShoppingCartRequest oneCheckoutShoppingCartRequest =
  GCheckoutButton1.CreateRequest();

oneCheckoutShoppingCartRequest.MerchantPrivateData = "clientid=3";

GCheckout.Checkout.ShoppingCartItem oneShoppingCartItem =
  new GCheckout.Checkout.ShoppingCartItem();
oneShoppingCartItem.Name = "YourProductDisplayName";
oneShoppingCartItem.MerchantItemID = "10";

oneCheckoutShoppingCartRequest.AddItem(oneShoppingCartItem);

The code will likely end up looking something like this:

GCheckout.Checkout.CheckoutShoppingCartRequest oneCheckoutShoppingCartRequest =
  GCheckoutButton1.CreateRequest();

oneCheckoutShoppingCartRequest.MerchantPrivateData = "clientid=3";

GCheckout.Checkout.ShoppingCartItem oneShoppingCartItem =
  new GCheckout.Checkout.ShoppingCartItem();
oneShoppingCartItem.Name = "YourProductDisplayName";
oneShoppingCartItem.MerchantItemID = "10";

oneCheckoutShoppingCartRequest.AddItem(oneShoppingCartItem);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文