.NET 开发淘宝API代码实例_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > .NET 开发淘宝API代码实例

.NET 开发淘宝API代码实例

 2010/11/2 10:51:37    程序员俱乐部  我要评论(0)
  • 摘要:这是一个通用的类。。具体调用看下面usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceTaoBaoManagersTools{publicclassTaoBaoManagersDataObject{publicstaticstringsip_sessionid=string.Empty;publicstaticstringformat=string.Empty
  • 标签:.NET 开发 淘宝API 实例
这是一个通用的类。。

具体调用 看下面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TaoBaoManagersTools
{
    public class TaoBaoManagersDataObject
    {
        public static string sip_sessionid = string.Empty;
        public static string format = string.Empty;
        public static string sip_appkey = string.Empty;
        public static string v = string.Empty;
        private static string sip_timestamp;
        private byte[] _address = null;
        private static ASCIIEncoding encoding;
        private static System.Net.HttpWebRequest myRequest;
        private System.Security.Cryptography.MD5CryptoServiceProvider md5;
        private StringBuilder sb;
        private static TaoBaoManagersDataObject instance = null;
        private static readonly object padlock = new object();
        /**/
        /// <summary>
        /// 定义TaoBaoDataObject的个单态以获取该类的一个实例
        /// </summary>
        public static TaoBaoManagersDataObject getTaoBaoManagersDataObject
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                        instance = new TaoBaoManagersDataObject();
                    return instance;
                }
            }
        }
        /// <summary>
        /// 设置店铺配置
        /// </summary>
        /// <param name="sce">ShopConfigEntity 实体</param>
        public void getShopConfig(TaoBaoManagersEntity.ShopConfigEntity sce)
        {
            sip_sessionid = sce.Sip_sessionid;
            format = sce.Format;
            sip_appkey = sce.Sip_appkey;
            v = sce.V;
        }
        /**/
        /// <summary>
        /// 设置参数
        /// </summary>
        public void setSL(System.Collections.SortedList mySL)
        {
            mySL.Clear();
            sip_timestamp = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            mySL.Add("format", format);
            mySL.Add("sip_appkey", sip_appkey);
            mySL.Add("sip_timestamp", sip_timestamp);
            mySL.Add("sip_sessionid", sip_sessionid);
            mySL.Add("v", v);
        }
        /**/
        /// <summary>
        /// 获取地址字符串
        /// </summary>
        /// <param name="mySL"></param>
        /// <returns></returns>
        public byte[] getAddress(System.Collections.SortedList mySL)
        {
            String address = "";
            for (int i = 0; i < mySL.Count; i++)
            {
                address = String.Format("{0}{1}={2}&", address, mySL.GetKey(i).ToString(), System.Web.HttpUtility.UrlEncode(mySL.GetByIndex(i).ToString()));
            }
            encoding = new ASCIIEncoding();
            _address = encoding.GetBytes(address + "sip_sign=" + getEncrypt(mySL));
            return _address;
        }

        /**/
        /// <summary>
        /// 对地址字符串进行MD5加密
        /// </summary>
        /// <param name="mySL">地址字符串数组</param>
        /// <returns></returns>
        public string getEncrypt(System.Collections.SortedList mySL)
        {
            //签名字段的生成,将CERT CODE和所有接口要传的参数进行组合,再将组装好的字符串进行md5加密后转成16进制后得到一个32位的密文
            //StringBuilder sb = new StringBuilder();
            sb = new StringBuilder();
            sb.Append("127966e01a9c11deb6daefc7091c0420"); //将安全编码放字符串首位
            //对list里的参数进行拼装,参数名+参数值,按自然排序,即所有参数字母排序
            foreach (System.Collections.DictionaryEntry Item in mySL)
            {
                sb.Append(Item.Key.ToString());
                if (Item.Value != null)
                {
                    sb.Append(Item.Value.ToString());
                }
            }
            md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(sb.ToString()))).Replace("-", "");//对拼装好的字符串进行md5加密
        }
        /// <summary>
        /// 对字符串进行MD5加密
        /// </summary>
        /// <param name="pToEntity"></param>
        /// <returns></returns>
        public string getEncrypt(string pToEntity)
        {
            //签名字段的生成,将CERT CODE和所有接口要传的参数进行组合,再将组装好的字符串进行md5加密后转成16进制后得到一个32位的密文
            //StringBuilder sb = new StringBuilder();
            sb = new StringBuilder();
            sb.Append("127966e01a9c11deb6daefc7091c0420"); //将安全编码放字符串首位
            sb.Append(pToEntity.ToString());
            return md5.ComputeHash(md5.ComputeHash(Encoding.UTF8.GetBytes(sb.ToString()))).ToString();
        }
        /// <summary>
  /// 加密算法
  /// </summary>
  /// <param name="pToEncrypt"></param>
  /// <param name="sKey"></param>
  /// <returns></returns>
  public string  getEncrypt(string  pToEncrypt,  string  sKey) 
  { 
   System.Security.Cryptography.DESCryptoServiceProvider  des  =  new  System.Security.Cryptography.DESCryptoServiceProvider(); 
   byte[]  inputByteArray  =  Encoding.Default.GetBytes(pToEncrypt);  
   des.Key  =  ASCIIEncoding.ASCII.GetBytes(sKey); 
   des.IV  =  ASCIIEncoding.ASCII.GetBytes(sKey); 
   System.IO.MemoryStream  ms  =  new  System.IO.MemoryStream(); 
   System.Security.Cryptography.CryptoStream  cs  =  new  System.Security.Cryptography.CryptoStream(ms,des.CreateEncryptor(),System.Security.Cryptography.CryptoStreamMode.Write); 
   cs.Write(inputByteArray,  0,  inputByteArray.Length); 
   cs.FlushFinalBlock();    
   StringBuilder  ret  =  new  StringBuilder(); 
   foreach(byte  b  in  ms.ToArray()) 
   { 
    //Format  as  hex 
    ret.AppendFormat("{0:X2}",  b); 
   } 
   ret.ToString(); 
   return  ret.ToString(); 
  }
  //解密算法
  public  string  getDecrypt(string  pToDecrypt,  string  sKey) 
  { 
   System.Security.Cryptography.DESCryptoServiceProvider  des  =  new  System.Security.Cryptography.DESCryptoServiceProvider();     
   byte[]  inputByteArray  =  new  byte[pToDecrypt.Length  /  2]; 
   for(int  x  =  0;  x  <  pToDecrypt.Length  /  2;  x++) 
   { 
    int  i  =  (Convert.ToInt32(pToDecrypt.Substring(x  *  2,  2),  16)); 
    inputByteArray[x]  =  (byte)i; 
   } 
   des.Key  =  ASCIIEncoding.ASCII.GetBytes(sKey); 
   des.IV  =  ASCIIEncoding.ASCII.GetBytes(sKey); 
   System.IO.MemoryStream  ms  =  new  System.IO.MemoryStream(); 
   System.Security.Cryptography.CryptoStream  cs  =  new  System.Security.Cryptography.CryptoStream(ms,des.CreateDecryptor(),System.Security.Cryptography.CryptoStreamMode.Write);   
   cs.Write(inputByteArray,  0,  inputByteArray.Length); 
   cs.FlushFinalBlock();    
   StringBuilder  ret  =  new  StringBuilder(); 
            
   return  System.Text.Encoding.Default.GetString(ms.ToArray()); 
  }
        /**/
        /// <summary>
        /// 向服务器发送地址字符串
        ///Prepare web request...
        ///目前阿里软件的服务集成平台(SIP)的接口测试地址是:
http://sipdev.alisoft.com/sip/rest,生产环境地址是:http://sip.alisoft.com/sip/rest,
        ///这里使用测试接口先,到正式上线时需要做切换
        /// </summary>
        /// <returns></returns>
        public void sendAddress()
        {
            System.IO.Stream newStream = getRequest().GetRequestStream();
            // Send address
            newStream.Write(_address, 0, _address.Length);
            newStream.Close();
        }
        /**/
        /// <summary>
        /// 获取Request对象
        /// </summary>
        /// <param name="myRequest"></param>
        /// <param name="mySL"></param>
        /// <returns></returns>
        public System.Net.HttpWebRequest getRequest()
        {
            myRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("
http://sip.alisoft.com/sip/rest");
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = _address.Length;
            return myRequest;
        }
        /**/
        /// <summary>
        /// 获取Response对象
        /// </summary>
        /// <param name="myRequest"></param>
        /// <param name="mySL"></param>
        /// <returns></returns>
        public System.Net.HttpWebResponse getResponse()
        {
            return (System.Net.HttpWebResponse)myRequest.GetResponse();
        }
        /**/
        /// <summary>
        /// 获取服务器发送回来的状态码
        /// </summary>
        /// <param name="mySL"></param>
        /// <returns></returns>
        public String getStatus()
        {
            return ((System.Net.HttpWebResponse)myRequest.GetResponse()).Headers.Get("sip_status");//获得接口返回状态码
        }
        /**/
        /// <summary>
        /// 获取服务器发送回来的数据
        /// </summary>
        /// <param name="myRequest"></param>
        /// <param name="mySL"></param>
        /// <returns></returns>
        public String getResource()
        {
            System.IO.StreamReader reader = new System.IO.StreamReader(getResponse().GetResponseStream(), Encoding.UTF8);
            return reader.ReadToEnd();
        }
        public string getResource(System.Collections.SortedList mySL)
        {
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getAddress(mySL);
           TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.sendAddress();
            return TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getResource();
        }
        /**/
        /// <summary>
        /// 自动添加参数列表到mySL
        /// </summary>
        /// <param name="mySL">System.Collections.SortedList</param>
        /// <param name="list">参数</param>
        public void addParamsTomySL(System.Collections.SortedList mySL, params object[] list)
        {
            for (int i = 0; i < list.Length / 2; i++)
            {
                //string a=list[2*i].ToString();
                //string b= list[(2 * i)+1].ToString();
                if (null != list[(2 * i) + 1])
                {
                    mySL.Add(list[2 * i], list[(2 * i) + 1]);
                }
            }
        }
        /**/
        /// <summary>
        /// 获取参数列表转换成Object数组
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public object[] getParams(params object[] list)
        {
            return list;
        }
        /**/
        /// <summary>
        /// 将Xml内容字符串转换成DataSet对象
        /// </summary>
        /// <param name="xmlStr">Xml内容字符串</param>
        /// <returns>DataSet对象</returns>
        public System.Data.DataSet CXmlToDataSet(string xmlStr)
        {
            if (!string.IsNullOrEmpty(xmlStr))
            {
                System.IO.StringReader StrStream = null;
                System.Xml.XmlTextReader Xmlrdr = null;
                try
                {
                    System.Data.DataSet ds = new System.Data.DataSet();
                    //读取字符串中的信息
                    StrStream = new System.IO.StringReader(xmlStr);
                    //获取StrStream中的数据
                    Xmlrdr = new System.Xml.XmlTextReader(StrStream);
                    //ds获取Xmlrdr中的数据               
                    ds.ReadXml(Xmlrdr);
                    return ds;
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                {
                    //释放资源
                    if (Xmlrdr != null)
                    {
                        Xmlrdr.Close();
                        StrStream.Close();
                        StrStream.Dispose();
                    }
                }
            }
            else
            {
                return null;
            }
        }
        /**/
        /// <summary>
        /// 把获取的xml文件转换成数据集System.Data.DataSet
        /// </summary>
        /// <param name="ds">System.Data.DataSet</param>
        /// <param name="xml">getResource:xml文件</param>
        /// <returns>System.Data.DataSet :数据集</returns>
        public System.Data.DataSet getDs(System.Data.DataSet ds, string xml)
        {
            //System.IO.FileStream fs = new System.IO.FileStream(xml, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            //System.IO.StreamReader sr = new System.IO.StreamReader(fs);
            //System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
            //xd.LoadXml(xml.ToString());
            ds.ReadXml(new System.IO.StringReader(xml));
            return ds;
        }
    }
}



调用接口的类

该类为调用 交易数据的类 其他类可以套用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TaoBaoManagersDAL
{
    public class TradeDao
    {
        private static System.Collections.SortedList mySL;
        private static TradeDao instance = null;
        private static readonly object padlock = new object();

        /**/
        /// <summary>
        /// 定义TradeDao的个单态以获取该类的一个实例
        /// </summary>
        public static TradeDao getTradeDao
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                        instance = new TradeDao();
                    return instance;
                }
            }
        }

        /**/
        /// <summary>
        /// 此接口方法用以搜索当前会话用户做为买家达成的交易记录。支持分页。不支持历史交易数据。
        /// </summary>
        /// <param name="fields">必须:需要返回的交易数据结构字段(不包括支付宝交易号及实付金额)</param>
        /// <param name="start_created">可选:查询交易创建时间开始</param>
        /// <param name="page_no">可选:页码</param>
        /// <param name="page_size">可选:每页条数</param>
        /// <param name="title">可选:商品名称</param>
        /// <param name="status">可选:交易状态</param>
        /// <param name="end_created">可选:查询交易创建时间结束</param>
        /// <returns>
        /// totalResults:搜索到的交易信息总数
        /// trade[]:搜索到的交易信息列表
        /// </returns>
        public string taobao_trades_bought_get(string fields, string start_created, string page_no, string page_size, string title, string status, string end_created)
        {
            mySL = new System.Collections.SortedList();
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.setSL(mySL);
            mySL.Add("sip_apiname", "taobao.trades.bought.get");
            object[] myParams = TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getParams("fields", fields, "start_created", start_created, "page_no", page_no, "page_size", page_size, "title", title, "status", status, "end_created", end_created);
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.addParamsTomySL(mySL, myParams);
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getAddress(mySL);
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.sendAddress();
            return TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getResource();
        }

        /**/
        /// <summary>
        /// 此接口方法用以搜索当前会话用户(做为卖家)已卖出的交易数据。支持分页。不支持历史交易数据。
        /// </summary>
        /// <param name="start_created">可选:查询交易创建时间开始</param>
        /// <param name="page_no">可选:页码</param>
        /// <param name="page_size">可选:每页条数</param>
        /// <param name="title">可选:商品名称</param>
        /// <param name="status">可选:交易状态</param>
        /// <param name="end_created">可选:查询交易创建时间结束</param>
        /// <param name="fields">需要返回的交易数据结构字段(不包括支付宝交易号及实付金额) </param>
        /// <returns>
        /// totalResults:搜索到的交易信息总数
        /// trade[]:搜索到的交易信息列表
        /// </returns>
        public string taobao_trades_sold_get(string start_created, string page_no, string page_size, string title, string status, string end_created, string fields)
        {
            mySL = new System.Collections.SortedList();
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.setSL(mySL);
            mySL.Add("sip_apiname", "taobao.trades.sold.get");
            object[] myParams = TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getParams("start_created", start_created, "page_no", page_no, "page_size", page_size, "title", title, "status", status, "end_created", end_created, "fields", fields);
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.addParamsTomySL(mySL, myParams);
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getAddress(mySL);
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.sendAddress();
            return TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getResource();
        }

        /**/
        /// <summary>
        /// 此接口方法用以搜索交易公开信息。支持分页。
        /// </summary>
        /// <param name="iid">必须:商品id</param>
        /// <param name="fields">必须:需要返回的交易数据结构字段(不包括支付宝交易号及实付金额) </param>
        /// <param name="seller_nike">必须:卖家昵称</param>
        /// <param name="page_no">可选:页码</param>
        /// <param name="page_size">可选:每页条数</param>
        /// <returns>
        /// totalResults:搜索到的交易信息总数
        /// trade[]:搜索到的交易信息列表
        /// </returns>
        public string taobao_trades_get(string iid, string fields, string seller_nike, string page_no, string page_size)
        {
            mySL = new System.Collections.SortedList();
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.setSL(mySL);
            mySL.Add("sip_apiname", "taobao.trades.get");
            object[] myParams = TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getParams("iid", iid, "fields", fields, "seller_nike", seller_nike, "page_no", page_no, "page_size", page_size);
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.addParamsTomySL(mySL, myParams);
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getAddress(mySL);
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.sendAddress();
            return TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getResource();
        }

        /**/
        /// <summary>
        /// 获取单条交易信息,包含支付宝交易号、实付金额及邮费
        /// </summary>
        /// <param name="fields">
        /// 必须:需要返回的交易数据结构字段
        /// seller_nick:卖家昵称,buyer_nick:买家昵称,title:商品标题(由于存在父子订单,这里返回的是店铺名称),type:交易类型,created:交易创建时间,sid:物流id,buyer_message:买家留言,
        /// tid:子订单id,seller_rate:卖家是否已评价,buyer_rate:买家是否已评价,
        /// status交易状态 等待买家付款:WAIT_BUYER_PA 未创建支付宝交易:TRADE_NO_CREATE_PAY 卖家已发货:WAIT_BUYER_CONFIRM_GOODS
        /// 买家已付款:WAIT_SELLER_SEND_GOODS 交易成功:TRADE_FINISHED,交易关闭:TRADE_CLOSED 创建关闭:TRADE_CLOSED_BY_TAOBAO
        /// 其他状态:OTHER,
        /// payment:实付金额,alipay_no:支付宝交易号,pay_time:付款时间,end_time:交易成功时间(tc更新交易状态为成功的同时更新),out_sid:外部物流订单号,modified:交易修改时间,
        /// buyer_obtain_point_fee:买家获得积分,返点的积分,buyer_memo:买家备注,seller_memo:卖家备注,point_fee:买家使用积分,real_point_fee:买家实际使用积分,total_fee:买家付款金额,orders:订单列表
        /// </param>
        /// <param name="tid">必须:交易ID</param>
        /// <returns></returns>
        public string taobao_trade_get(string fields, string tid)
        {
            mySL = new System.Collections.SortedList();
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.setSL(mySL);
            mySL.Add("sip_apiname", "taobao.trade.get");
            object[] myParams = TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getParams("fields", fields, "tid", tid);
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.addParamsTomySL(mySL, myParams);
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getAddress(mySL);
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.sendAddress();
            return TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getResource();
        }

        /**/
        /// <summary>
        /// 查询某商品的出价记录
        /// </summary>
        /// <param name="fiekds">
        /// 必须:需要返回的交易数据结构字段
        /// seller_nick:卖家昵称,buyer_nick:买家昵称,title:商品标题(由于存在父子订单,这里返回的是店铺名称),
        /// type:交易类型,created:交易创建时间,iid:商品id,price:商品价格,pic_path:商品图片路径,num:购买数量
        /// </param>
        /// <param name="iid">必须:商品id</param>
        /// <param name="seller_nike">可选:卖家昵称</param>
        /// <param name="pae_no">可选;页码</param>
        /// <param name="page_size">可选;每页条数</param>
        /// <returns></returns>
        public string taobao_orders_get(string fields, string iid, string seller_nike, string pae_no, string page_size)
        {
            mySL = new System.Collections.SortedList();
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.setSL(mySL);
            mySL.Add("sip_apiname", "taobao.orders.get");
            object[] myParams = TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getParams("fields", fields, "iid", iid, "seller_nike", seller_nike, "pae_no", pae_no, "page_size", page_size);
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.addParamsTomySL(mySL, myParams);
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getAddress(mySL);
            TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.sendAddress();
            return TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.getResource();
        }

    }
}

页面调用 是这样的,,,,

这个是个空方法 可以任意调用

其他方法 都可以套用
 /**/
        /// <summary>
        /// 获取买家已付款的数据量
        /// </summary>

          public void getWAIT_SELLER_SEND_GOODS()
        {
            string xmlSouse = TaoBaoManagersDAL.TradeDao.getTradeDao.taobao_trades_sold_get(null, null, null, null, "WAIT_SELLER_SEND_GOODS", null, fields);
            System.Data.DataSet ds = TaoBaoManagersTools.TaoBaoManagersDataObject.getTaoBaoManagersDataObject.CXmlToDataSet(xmlSouse);
            this.WAIT_SELLER_SEND_GOODS.Text = ds.Tables[0].Rows[0]["totalResults"].ToString();
        }
发表评论
用户名: 匿名