淘宝开发平台实例代码

package test;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.taobao.api.TaobaoApiException;
import com.taobao.api.TaobaoJsonRestClient;
import com.taobao.api.TaobaoRestClient;
import com.taobao.api.model.ItemsGetResponse;
import com.taobao.api.model.ItemsOnSaleGetRequest;

/**
 * @version 2008-10-29
 * @author
 *
 */
public class MainServlet extends HttpServlet {
 private static final transient Log log = LogFactory
   .getLog(MainServlet.class);

 /**
  *
  */
 private static final long serialVersionUID = 8839099738909766274L;

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  String appkey = req.getParameter("appkey");
  String secret = req.getParameter("secret");
  if (log.isDebugEnabled()) {
   log.debug(appkey + ":" + secret);
  }
  if (appkey != null && secret != null) {
   try {
    // 生成客户端实例
    TaobaoRestClient client = new TaobaoJsonRestClient(appkey,
      secret);
    // 淘宝卖家的出售的商品列表
    ItemsOnSaleGetRequest tbReq = new ItemsOnSaleGetRequest()
      .withFields("iid,title,price,list_time,num")
      .withPageNo(1).withPageSize(20);
    // use appkey as session_id
    ItemsGetResponse tbRsp = client.itemsOnSaleGet(tbReq, appkey);// 搜索单个商品信息的Request请求
    if (log.isDebugEnabled()) {
     log.debug(tbRsp.getBody());
    }
    // 如果接口调用成功
    if (!tbRsp.isSuccess()) {
     if (tbRsp.isRedirect()) {
      req.setAttribute("url", tbRsp.getRedirectUrl(URLEncoder
        .encode(req.getRequestURL().append('?').append(
          "appkey=").append(appkey).append(
          "&secret=").append(secret).toString(),
          "utf-8")));
     } else {
      req.setAttribute("msg", tbRsp.getMsg());
     }
    } else {
     req.setAttribute("items", tbRsp.getItems());
    }
   } catch (TaobaoApiException e) {
    throw new ServletException(e);
   }
  }
  req.getRequestDispatcher("main.jsp").forward(req, resp);
 }

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  doPost(req, resp);
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; using System.Security.Cryptography; /// <summary> /// New Interface for AliPay /// </summary> namespace Gateway { /// <summary> /// created by sunzhizhi 2006.5.21,sunzhizhi@msn.com。 /// </summary> public class AliPay { public static string GetMD5(string s) { /// <summary> /// 与ASP兼容的MD5加密算法 /// </summary> MD5 md5 = new MD5CryptoServiceProvider(); byte[] t = md5.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(s)); StringBuilder sb = new StringBuilder(32); for (int i = 0; i < t.Length; i++) { sb.Append(t[i].ToString("x").PadLeft(2, '0')); } return sb.ToString(); } public static string[] BubbleSort(string[] r) { /// <summary> /// 冒泡排序法 /// </summary> int i, j; //交换标志 string temp; bool exchange; for (i = 0; i < r.Length; i++) //最多做R.Length-1趟排序 { exchange = false; //本趟排序开始前,交换标志应为假 for (j = r.Length - 2; j >= i; j--) { if (System.String.CompareOrdinal(r[j + 1], r[j]) < 0) //交换条件 { temp = r[j + 1]; r[j + 1] = r[j]; r[j] = temp; exchange = true; //发生了交换,故将交换标志置为真 } } if (!exchange) //本趟排序未发生交换,提前终止算法 { break; } } return r; } public string CreatUrl( string gateway, string service, string partner, string sign_type, string out_trade_no, string subject, string body, string quantity, string price, string show_url, string seller_email, string key, string return_url, string notify_url, string logistics_type, string logistics_fee, string logistics_payment, string logistics_type_1, string logistics_fee_1, string logistics_payment_1, string payment_type ) { /// <summary> /// created by sunzhizhi 2006.5.21,sunzhizhi@msn.com。 /// </summary> int i; //构造数组; string[] Oristr ={ "service="+service, "partner=" + partner, "subject=" + subject, "body=" + body, "out_trade_no=" + out_trade_no, "price=" + price, "show_url=" + show_url, "quantity=" + quantity, "seller_email=" + seller_email, "notify_url=" + notify_url, "return_url=" + return_url, "logistics_type="+logistics_type, "logistics_fee="+logistics_fee , "logistics_payment="+logistics_payment, "logistics_type_1="+logistics_type_1, "logistics_fee_1="+logistics_fee_1, "logistics_payment_1="+logistics_payment_1, "payment_type=" + payment_type }; //进行排序; string[] Sortedstr = BubbleSort(Oristr); //构造待md5摘要字符串 ; StringBuilder prestr = new StringBuilder(); for (i = 0; i < Sortedstr.Length; i++) { if (i == Sortedstr.Length - 1) { prestr.Append(Sortedstr[i]); } else { prestr.Append(Sortedstr[i] + "&"); } } prestr.Append(key); //生成Md5摘要; string sign = GetMD5(prestr.ToString()); //构造支付Url; StringBuilder parameter = new StringBuilder(); parameter.Append(gateway); for (i = 0; i < Sortedstr.Length; i++) { parameter.Append(Sortedstr[i] + "&"); } parameter.Append("sign=" + sign + "&sign_type=" + sign_type); //返回支付Url; return parameter.ToString(); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值