Silverlight無法直接調用淘寶的API,可以使用WebClient或者自訂WCF來間接調用淘寶介面。
本例中採用的是調用WebClient方式,執行一段ashx,然後將返回的資料用反序列化生成相應物件的實例。
起始頁面為Login.aspx,固定調試埠為49441。需要配合自己淘寶開放平臺的應用的回檔頁面URL來調整。
示例代碼下載
ashx代碼:
(說明:代碼中ITopClient為淘寶介面TopSdk.dll中的類,此例子使用的ItemsOnsaleGetRequest是用於獲取銷售中的商品,response.Body是獲取到的資料資訊)
public class OnsaleGet : IHttpHandler
{

 

public void ProcessRequest(HttpCoNtext coNtext)
{
coNtext.Response.ContentType = "text/plain";

 

ITopClient client = new DefaultTopClient(Config.ServerURL, Config.Appkey, Config.Secret);

 

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);



if (response.IsError)
{
coNtext.Response.Write("[錯誤:查詢函數執行失敗]");
}
else
{
coNtext.Response.Write(response.Body);
}

 

}

 

public bool IsReusable
{
get
{
return false;
}
}
}

 

前端SL獲取資料資訊的方法
void GetList()
{
string absolutePath = HtmlPage.Document.DocumentUri.AbsoluteUri;
string address = absolutePath.Substring(0, absolutePath.LastIndexOf('/'))
+ "/TaoBaoHandler/OnsaleGet.ashx";

 

Uri uri = new Uri(address);

 

WebClient client = new WebClient();
client.DownloadStringCompleted += (sender, e) =>
{
if (e.Error == null)
{
System.Xml.Linq.XElement.Parse(e.Result);//字串轉為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
序列化部分是自訂的一個類
public class SerializeHelper
{
private SerializeHelper() { }

 

#region Serialize

 

/// <summary>
/// 序列化實體
/// </summary>
/// <typeparam name="T">實體類型</typeparam>
/// <param name="data">實體</param>
/// <returns>xml字體串</returns>
public static string Serialize<T>(T data)
{
try
{
var serializer = new XmlSerializer(typeof(T));

 

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();

 

return xmlText;
}
catch (InvalidOperationException ex)
{
if (ex.Message != "")
{ }
}

 

return string.Empty;
}

 

public static string SerializeList<T>(List<T> list)
{
try
{
var serializer = new XmlSerializer(typeof(List<T>));

 

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();

 

return xmlText;
}
catch
{ }

 

return string.Empty;
}

 

#endregion

 

#region Deserializer

 

/// <summary>
/// 反序列化實體
/// </summary>
/// <typeparam name="T">實體類型</typeparam>
/// <param name="xml">xml字體串</param>
/// <returns>實體</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
{ }

return default(T);
}

public static List<T> DeserializeListFromString<T>(string xml)
{
try
{
var serializer = new XmlSerializer(typeof(List<T>));
StringReader reader = new StringReader(xml);

List<T> list = (List<T>)serializer.Deserialize(reader);
reader.Close();

return list;
}
catch (InvalidOperationException ex)
{
if (ex.InnerException.Message != "")
{ }
}

return null;
}

#endregion
}

淘寶相關的資料類型
TopModels模組中的ItemsOnsaleGetResponse,Item,ItemImg,Location等這些實體是從TopSdk.dll中找到相應的定義粘貼出來供Silverlight下使用的。如圖
1019106344-0  
web.config中的配置
配置沙箱環境和正式環境的選擇,以及AppKey和AppSecret
<appSettings>

<!--IsSandBox 1=沙箱開啟 0=非沙箱(正式環境) -->
<add key="SandBox" value="1"/>
<!--應用 資訊-->
<add key="AppKey" value="1012596959"/>
<add key="AppSecret" value="sandboxc5928dd8d1cfa3bb4f7d87e33"/>

</appSettings>

另外,web中的那些Client.cs和Config.cs則是從淘寶示例Demo中來的。
最終資料的獲取如下:
 1019101360-1  
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()