您的位置:首页 > 编程语言 > C#

阿里软件接口开发基础(淘宝网) C#

2011-07-23 14:04 246 查看
主要开发文件见:

http://wiki.isv.alisoft.com/index.php?tracelog=doc_from_home

当前J***A例子比较多,C#比较少,

下面提供本人开发一些例子:

向服务器发送请求类:
public static XmlDocument HttpRequest(string data)
{
//ASCIIEncoding encoding = new ASCIIEncoding();
byte[] postdata = System.Text.Encoding.UTF8.GetBytes(data);//所有要传参数拼装
// Prepare web request
//目前阿里软件的服务集成平台(SIP)的接口测试地址是:http://sipdev.alisoft.com/sip/rest,生产环境地址是:http://sip.alisoft.com/sip/rest,
//这里使用测试接口先,到正式上线时需要做切换
string url = System.Configuration.ConfigurationManager.AppSettings["APPURL"];
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = postdata.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(postdata, 0, postdata.Length);
newStream.Close();
// Get response
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(reader.ReadToEnd());
XmlNode node = xmlDoc.SelectSingleNode("/error_rsp/code");
if (node != null && node.InnerText != string.Empty)
{

throw new ApiException(node.InnerText, xmlDoc.SelectSingleNode("/error_rsp/msg").InnerText);
}
return xmlDoc;

}
public static string HttpRequest(string data, string xPath)
{
XmlDocument doc = HttpRequest(data);
return doc.SelectSingleNode(xPath).InnerText;
}
public static string MD5(string data)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(data))).Replace("-", "");
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
对参数进行排序类:

对参数时行排序

public class ParamsBuild
{
string _code { get; set; }
SortedList _mySL = new SortedList();

public ParamsBuild(string code)
{
_code = code;
}
public ParamsBuild(System.Web.HttpContext content, string apiName)
: this(content.Session.SessionID,apiName)
{

}
public ParamsBuild(string sip_sessionid, string apiName)
: this(Util.GetCode)
{
AddParam("sip_appkey", Util.GetAppID);
AddParam("sip_apiname", apiName);
AddParam("sip_timestamp", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
AddParam("sip_sessionid", sip_sessionid);
}
public void AddParam(string name, string value)
{
_mySL.Add(name, value);
}
public void AddParam(string name, int value)
{
_mySL.Add(name, value);
}
public string GetURL()
{
StringBuilder orgin = new StringBuilder();
orgin.Append(_code); //将安全编码放字符串首位
//对list里的参数进行拼装,参数名+参数值,按自然排序,即所有参数字母排序
StringBuilder _url = new StringBuilder();
foreach (DictionaryEntry Item in _mySL)
{
// ListItem newListItem = new ListItem();
orgin.Append(Item.Key.ToString());
if (Item.Value != null)
{
orgin.Append(Item.Value.ToString());
}
_url.AppendFormat("{0}={1}&", Item.Key, Item.Value);

}
_url.AppendFormat("sip_sign={0}", Util.MD5(orgin.ToString()));
return _url.ToString();
}
}
public static string GetAppID
{
get
{
return System.Configuration.ConfigurationManager.AppSettings["APPID"];
}
}
public static string GetCode
{
get
{
//软件注册时获得
return System.Configuration.ConfigurationManager.AppSettings["APPcode"];
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
淘宝相关的一个操作类:
TAOBAO EXAMPLE

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Xml;
namespace AliSoftApi
{

public class taobaoApi
{
public const string VERSION = "1.0";

private string _sessionID;

public taobaoApi()
{
}
public taobaoApi(string sessionID)
{
_sessionID = sessionID;
}
private string SessionID
{
get
{
if (!string.IsNullOrEmpty(_sessionID))
{
return _sessionID;
}
else
{
return HttpContext.Current.Session.SessionID;
}
}
}

public XmlDocument taobao_items_get(string q, string fields, int page_no, int page_size, string nicks)
{

Util.ParamsBuild pb = new Util.ParamsBuild(SessionID, "taobao.items.get");

pb.AddParam("fields", fields);//"iid,delist_time"
pb.AddParam("v", VERSION);
if (q != string.Empty)
pb.AddParam("q", q);
if(page_no != 0)
pb.AddParam("page_no", page_no);
if(page_size != 0)
pb.AddParam("page_size", page_size);
// /
pb.AddParam("nicks", nicks);
//if(order_by != "")
// pb.AddParam("order_by", order_by);
string data = pb.GetURL();

return Util.HttpRequest(data);
}
public XmlDocument taobao_items_instock_get(string q, string fields, int page_no, int page_size, bool has_discount, bool has_showcase)
{

Util.ParamsBuild pb = new Util.ParamsBuild(SessionID, "taobao.items.instock.get");

pb.AddParam("fields", fields);//"iid,delist_time"
pb.AddParam("v", VERSION);
if (q != string.Empty)
pb.AddParam("q", q);
if (page_no != 0)
pb.AddParam("page_no", page_no);
if (page_size != 0)
pb.AddParam("page_size", page_size);
if (has_discount)
{
pb.AddParam("has_discount", "true");
}
if (has_showcase)
{
pb.AddParam("has_showcase", "true");
}
string data = pb.GetURL();

return Util.HttpRequest(data);
}

public XmlDocument taobao_items_onsale_get(string q, string fields, int page_no, int page_size, bool has_discount, bool has_showcase)
{

Util.ParamsBuild pb = new Util.ParamsBuild(SessionID, "taobao.items.onsale.get");

pb.AddParam("fields", fields);//"iid,delist_time"
pb.AddParam("v", VERSION);
if (q != string.Empty)
pb.AddParam("q", q);
if (page_no != 0)
pb.AddParam("page_no", page_no);
if (page_size != 0)
pb.AddParam("page_size", page_size);
if (has_discount)
{
pb.AddParam("has_discount", "true");
}
if (has_showcase)
{
pb.AddParam("has_showcase", "true");
}
string data = pb.GetURL();
return Util.HttpRequest(data);
}
/// <summary>
/// 橱窗
/// </summary>
/// <param name="iid"></param>
/// <returns></returns>
public bool taobao_item_update_showcase(string iid)
{

Util.ParamsBuild pb = new Util.ParamsBuild(SessionID, "taobao.item.update.showcase");

pb.AddParam("iid", iid);
pb.AddParam("v", VERSION);
string data = pb.GetURL();
return Util.HttpRequest(data, "//iid") == iid;
}
/// <summary>
/// 取消橱窗
/// </summary>
/// <param name="iid"></param>
/// <returns></returns>
public bool taobao_item_update_revokeShowcase(string iid)
{

Util.ParamsBuild pb = new Util.ParamsBuild(SessionID, "taobao.item.update.revokeShowcase");

pb.AddParam("iid", iid);
pb.AddParam("v", VERSION);
string data = pb.GetURL();
return Util.HttpRequest(data, "//iid") == iid;
}
/// <summary>
/// 上架
/// </summary>
/// <param name="iid"></param>
/// <returns></returns>
public bool taobao_item_update_listing(string iid)
{
Util.ParamsBuild pb = new Util.ParamsBuild(SessionID, "taobao.item.update.listing");
pb.AddParam("iid", iid);
pb.AddParam("v", VERSION);
string data = pb.GetURL();
return Util.HttpRequest(data, "//iid") == iid;
}
/// <summary>
/// 下架
/// </summary>
/// <param name="iid"></param>
/// <returns></returns>
public bool taobao_item_update_delisting(string iid)
{

Util.ParamsBuild pb = new Util.ParamsBuild(SessionID, "taobao.item.update.delisting");

pb.AddParam("iid", iid);
pb.AddParam("v", VERSION);
string data = pb.GetURL();
return Util.HttpRequest(data, "//iid") == iid;
}
/// <summary>
/// 商品修改
/// </summary>
/// <param name="iid"></param>
/// <param name="num"></param>
/// <param name="title"></param>
/// <param name="price"></param>
/// <returns></returns>
public bool taobao_item_update(string iid, string num, string title, string price)
{

Util.ParamsBuild pb = new Util.ParamsBuild(SessionID, "taobao.item.update");

pb.AddParam("iid", iid);//"iid,delist_time"
pb.AddParam("v", VERSION);
if (num != string.Empty)
pb.AddParam("num", num);
if (title != string.Empty)
pb.AddParam("title", title);
if (price != string.Empty)
pb.AddParam("price", price);

string data = pb.GetURL();

return Util.HttpRequest(data, "//iid") == iid;
}

}
}

Demo: http://u.115.com/file/aqbpjvru# AliSoftApi.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: