silverlight调用淘宝api接口做淘宝客应用

网友投稿 300 2022-06-07


起始页面为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(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

///

/// 序列化实体 /// /// 实体类型 /// 实体 /// xml字体串 public static string Serialize(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(List list) { try { var serializer = new XmlSerializer(typeof(List));

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

///

/// 反序列化实体 /// /// 实体类型 /// xml字体串 /// 实体 public static T DeserializeFromString(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 DeserializeListFromString(string xml) { try { var serializer = new XmlSerializer(typeof(List)); StringReader reader = new StringReader(xml);

List list = (List)serializer.Deserialize(reader); reader.Close();

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

return null; }

#endregion }

web.config中的配置配置沙箱环境和正式环境的选择,以及AppKey和AppSecret

复制代码代码如下:

另外,web中的那些Client.cs和Config.cs则是从淘宝示例Demo中来的。

最终数据的获取如下:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:html5中几个不容错过的api或者tips小结(html5提供的api有)
下一篇:Excel中怎么查看API? Excel2010查看api的方法(excel中怎么查看重复信息)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~