ASP.NET挪用淘宝API实例详解
本文和各人具体分享一下如何使用ASP.NET来调用淘宝客API开发的实例讲解。
第一步 获取全部推广商品实体
需要使用类 ITopClient TOP客户端类
1using Top.Api.Request;
2
3namespace Top.Api
4 {
5///<summary>
6/// TOP客户端。
7///</summary>
8publicinte***ce ITopClient
9 {
10///<summary>
11/// 执行TOP公然API请求。
12///</summary>
13///<typeparam name="T">领域对象</typeparam>
14///<param name="request">具体的TOP API请求</param>
15///<returns>领域对象</returns>
16 T Execute<T>(ITopRequest<T> request) where T : TopResponse;
17
18///<summary>
19/// 执行TOP隐私API请求。
20///</summary>
21///<typeparam name="T">领域对象</typeparam>
22///<param name="request">具体的TOP API请求</param>
23///<param name="session">用户会话码</param>
24///<returns>领域对象</returns>
25 T Execute<T>(ITopRequest<T> request, string session) where T : TopResponse;
26 }
27 }
DefaultTopClient 初始化连接(请求地址, Appkey, AppSecret);
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using Top.Api.Parser;
5using Top.Api.Request;
6using Top.Api.Util;
7
8namespace Top.Api
9 {
10///<summary>
11/// 基于REST的TOP客户端。
12///</summary>
13publicclass DefaultTopClient : ITopClient
14 {
15publicconststring APP_KEY ="app_key";
16publicconststring FORMAT ="format";
17publicconststring METHOD ="method";
18publicconststring TIMESTAMP ="timestamp";
19publicconststring VERSION ="v";
20publicconststring SIGN ="sign";
21publicconststring PARTNER_ID ="partner_id";
22publicconststring SESSION ="session";
23publicconststring FORMAT_XML ="xml";
24
25privatestring serverUrl;
26privatestring appKey;
27privatestring appSecret;
28privatestring format = FORMAT_XML;
29
30private WebUtils webUtils;
31
32#region DefaultTopClient Constructors
33
34public DefaultTopClient(string serverUrl, string appKey, string appSecret)
35 {
36this.appKey = appKey;
37this.appSecret = appSecret;
38this.serverUrl = serverUrl;
39this.webUtils =new WebUtils();
40 }
41
42public DefaultTopClient(string serverUrl, string appKey, string appSecret, string format)
43 : this(serverUrl, appKey, appSecret)
44 {
45this.format = format;
46 }
47
48publicvoid SetTimeout(int timeout)
49 {
50 webUtils.Timeout = timeout;
51 }
52
53#endregion
54
55#region ITopClient Members
56
57public T Execute<T>(ITopRequest<T> request) where T : TopResponse
58 {
59return Execute<T>(request, null);
60 }
61
62public T Execute<T>(ITopRequest<T> request, string session) where T : TopResponse
63 {
64//添加协议级请求参数
65 TopDictionary txtParams =new TopDictionary(request.GetParameters());
66 txtParams.Add(METHOD, request.GetApiName());
67 txtParams.Add(VERSION, "2.0");
68 txtParams.Add(APP_KEY, appKey);
69 txtParams.Add(FORMAT, format);
70 txtParams.Add(PARTNER_ID, "top-sdk-net-");
71 txtParams.Add(TIMESTAMP, DateTime.Now);
72 txtParams.Add(SESSION, session);
73
74// 添加签名参数
75 txtParams.Add(SIGN, TopUtils.SignTopRequest(txtParams, appSecret));
76
77// 是否需要上传文件
78string body;
79if (request is ITopUploadRequest<T>)
80 {
81 ITopUploadRequest<T> uRequest = (ITopUploadRequest<T>)request;
82 IDictionary<string, FileItem> fileParams = TopUtils.CleanupDictionary(uRequest.GetFileParamet ers());
83 body = webUtils.DoPost(this.serverUrl, txtParams, fileParams);
84 }
85else
86 {
87 body = webUtils.DoPost(this.serverUrl, txtParams);
88 }
89
90 T rsp;
91if (FORMAT_XML.Equals(format))
92 {
93 ITopParser<T> tp =new TopXmlParser<T>();
94 rsp = tp.Parse(body);
95 }
96else
97 {
98 ITopParser<T> tp =new TopJsonParser<T>();
99 rsp = tp.Parse(body);
100 }
101
102return rsp;
103 }
104
105#endregion
106 }
107 }
TaobaokeItemsGetResponse 信息返回
1using System;
2using System.Xml.Serialization;
3using System.Collections.Generic;
4using Top.Api.Domain;
5
6namespace Top.Api.Response
7 {
8///<summary>
9/// TaobaokeItemsGetResponse.
10///</summary>
11publicclass TaobaokeItemsGetResponse : TopResponse
12 {
13///<summary>
14/// 淘宝客商品对象列表。不返回taobaoke_cat_click_url和keyword_click_url两个字段。
15///</summary>
16 [XmlArray("taobaoke_items")]
17 [XmlArrayItem("taobaoke_item")]
18public List<TaobaokeItem> TaobaokeItems { get; set; }
19
20///<summary>
21/// 搜索到符合条件
补充:Web开发 , ASP.Net ,