silverlight調(diào)用淘寶api接口做淘寶客應(yīng)用

起始頁(yè)面為L(zhǎng)ogin.aspx,固定調(diào)試端口為49441。需要配合自己淘寶開(kāi)放平臺(tái)的應(yīng)用的回調(diào)頁(yè)面URL來(lái)調(diào)整。
ashx代碼:
(說(shuō)明:代碼中ITopClient為淘寶接口TopSdk.dll中的類,此例子使用的ItemsOnsaleGetRequest是用于獲取銷售中的商品,response.Body是獲取到的數(shù)據(jù)信息)
public class OnsaleGet : IHttpHandler
{</p> <p> public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
ITopClient client = new DefaultTopClient(Config.ServerURL, Config.Appkey, Config.Secret);</p> <p> ItemsOnsaleGetRequest req = new ItemsOnsaleGetRequest();
req.Fields = "approve_status,num_iid,title,nick,type,cid,pic_url,num,props,valid_thru,list_time,price,has_discount,has_invoice,has_warranty,has_showcase,modified,delist_time,postage_id,seller_cids,outer_id";
ItemsOnsaleGetResponse response = client.Execute(req, Config.Top_session);</p> <p>
if (response.IsError)
{
context.Response.Write("[錯(cuò)誤:查詢函數(shù)執(zhí)行失敗]");
}
else
{
context.Response.Write(response.Body);
}</p> <p> }</p> <p> public bool IsReusable
{
get
{
return false;
}
}
}
前端SL獲取數(shù)據(jù)信息的方法
void GetList()
{
string absolutePath = HtmlPage.Document.DocumentUri.AbsoluteUri;
string address = absolutePath.Substring(0, absolutePath.LastIndexOf('/'))
+ "/TaoBaoHandler/OnsaleGet.ashx";</p> <p> Uri uri = new Uri(address);</p> <p> WebClient client = new WebClient();
client.DownloadStringCompleted += (sender, e) =>
{
if (e.Error == null)
{
System.Xml.Linq.XElement.Parse(e.Result);//字符串轉(zhuǎn)為xml
ItemsOnsaleGetResponse list = SerializeHelper.DeserializeFromString<ItemsOnsaleGetResponse>(e.Result);//反序列化
if (list != null)
{
if (list.Items != null && list.Items.Count > 0)
{
MessageBox.Show(list.Items[0].NumIid.ToString());
}
}
else
{
}
}
else
{
MessageBox.Show(e.Error.Message);
}
};
client.DownloadStringAsync(uri);
}
SerializeHelper
序列化部分是自定義的一個(gè)類
public class SerializeHelper
{
private SerializeHelper() { }</p> <p> #region Serialize</p> <p> /// <summary>
/// 序列化實(shí)體
/// </summary>
/// <typeparam name="T">實(shí)體類型</typeparam>
/// <param name="data">實(shí)體</param>
/// <returns>xml字體串</returns>
public static string Serialize<T>(T data)
{
try
{
var serializer = new XmlSerializer(typeof(T));</p> <p> XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.OmitXmlDeclaration = true;
StringWriter stringWriter = new StringWriter();
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, writerSettings))
{
serializer.Serialize(xmlWriter, data, ns);
}
string xmlText = stringWriter.ToString();</p> <p> return xmlText;
}
catch (InvalidOperationException ex)
{
if (ex.Message != "")
{ }
}</p> <p> return string.Empty;
}</p> <p> public static string SerializeList<T>(List<T> list)
{
try
{
var serializer = new XmlSerializer(typeof(List<T>));</p> <p> XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.OmitXmlDeclaration = true;
StringWriter stringWriter = new StringWriter();
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, writerSettings))
{
serializer.Serialize(xmlWriter, list, ns);
}
string xmlText = stringWriter.ToString();</p> <p> return xmlText;
}
catch
{ }</p> <p> return string.Empty;
}</p> <p> #endregion</p> <p> #region Deserializer</p> <p> /// <summary>
/// 反序列化實(shí)體
/// </summary>
/// <typeparam name="T">實(shí)體類型</typeparam>
/// <param name="xml">xml字體串</param>
/// <returns>實(shí)體</returns>
public static T DeserializeFromString<T>(string xml)
{
T theObject;
try
{
XmlReader reader = XmlReader.Create(new StringReader(xml));
var serializer = new XmlSerializer(typeof(T));
theObject = (T)serializer.Deserialize(reader);
reader.Close();
return theObject;
}
catch
{ }</p> <p> return default(T);
}</p> <p> public static List<T> DeserializeListFromString<T>(string xml)
{
try
{
var serializer = new XmlSerializer(typeof(List<T>));
StringReader reader = new StringReader(xml);</p> <p> List<T> list = (List<T>)serializer.Deserialize(reader);
reader.Close();</p> <p> return list;
}
catch (InvalidOperationException ex)
{
if (ex.InnerException.Message != "")
{ }
}</p> <p> return null;
}</p> <p> #endregion
}
淘寶相關(guān)的數(shù)據(jù)類型
TopModels模塊中的ItemsOnsaleGetResponse,Item,ItemImg,Location等這些實(shí)體是從TopSdk.dll中找到相應(yīng)的定義粘貼出來(lái)供Silverlight下使用的。如圖
web.config中的配置
配置沙箱環(huán)境和正式環(huán)境的選擇,以及AppKey和AppSecret
<appSettings>
<!--IsSandBox 1=沙箱開(kāi)啟 0=非沙箱(正式環(huán)境) -->
<add key="SandBox" value="1"/>
<!--應(yīng)用 信息-->
<add key="AppKey" value="1012596959"/>
<add key="AppSecret" value="sandboxc5928dd8d1cfa3bb4f7d87e33"/></p> <p> </appSettings>
另外,web中的那些Client.cs和Config.cs則是從淘寶示例Demo中來(lái)的。
最終數(shù)據(jù)的獲取如下:
相關(guān)文章
WebStorm如何調(diào)試Vue項(xiàng)目? webstorm配置vue開(kāi)發(fā)環(huán)境指南
WebStorm 支持多種調(diào)試工具,包括瀏覽器的開(kāi)發(fā)者工具,但本文主要講解的是使用 WebStorm 自帶的調(diào)試功能2025-04-15VSCode和webstorm怎么設(shè)置綠色護(hù)眼背景? 綠豆沙背景色的設(shè)置方法
護(hù)眼色一定程度能保護(hù)眼睛,緩解疲勞,VSCode和webstorm這兩款常用的軟件怎么設(shè)置護(hù)眼色呢?詳細(xì)請(qǐng)看下文介紹2025-04-15WebStorm常用插件以及實(shí)用設(shè)置分享
WebStorm本身已經(jīng)足夠強(qiáng)大,但一些優(yōu)秀的插件能錦上添花,顯著提升開(kāi)發(fā)效率,詳細(xì)請(qǐng)看下文介紹2025-04-15如何安裝配置WebStorm? WebStorm安裝與使用全方位指南
WebStorm軟件在前端和后端開(kāi)發(fā)領(lǐng)域都備受青睞,很多朋友不知道該怎么下載安裝,下面我們就來(lái)看看詳細(xì)的安裝配置教程2025-04-15Webstorm怎么配置? Webstorm入門之軟件配置教程
WebStorm是一款功能強(qiáng)大的集成開(kāi)發(fā)環(huán)境(IDE),支持各種前端開(kāi)發(fā)技術(shù),今天我們就來(lái)看看Webstorm的配置教程2025-04-15Webstorm怎么設(shè)置字體大小/背景顏色/背景圖片?
WebStorm 允許您自定義界面顏色,以創(chuàng)建更個(gè)性化和美觀的工作空間,今天我們就來(lái)看看Webstorm改變字體大小、背景顏色、設(shè)置背景圖片的方法2025-04-15VScode上ESP32開(kāi)發(fā)插件安裝失敗怎么辦?
VScode上安裝PlatformIO插件能成功安裝,嘗試卸載VScode并刪除插件,這些錯(cuò)誤仍然存在,下面我們就來(lái)看看這個(gè)問(wèn)題的解決辦法2025-04-03提升代碼搜索效率! VSCode里DeepSeek插件安裝與配置指南
今天我們將向大家介紹如何在Visual Studio Code中安裝并配置 DeepSeek 插件,幫助你更高效地進(jìn)行代碼搜索2025-04-07提升你的編程效率! VSCode的初級(jí)使用教程超詳細(xì)版
VSCode是一款免費(fèi)且開(kāi)源的代碼編輯器,因其強(qiáng)大的功能和良好的用戶體驗(yàn)而廣受歡迎,本文將詳細(xì)介紹 VSCode 的基本使用方法,并通過(guò)插圖幫助你更好地理解2025-04-03- 今天我們來(lái)聊聊如何安裝和配置VS Code,讓它成為你編程路上的得力助手,這個(gè)過(guò)程其實(shí)很簡(jiǎn)單,只要跟著我的步驟走,你絕對(duì)能搞定2025-04-03