京東聯盟C#接口的下載地址為: http://jos.jd.com/doc/channel.htm?id=285
下載后,默認是一個控制臺程序,核心庫和demo程序在一個項目中。這里我把核心庫獨立成了Dll項目。
接口使用流程是,初始化DefaultJdClient類,然后調用需要的接口類,傳入參數,執行獲取返回結果。
注意,使用前請先修改bin目錄下的config.json文件,配置appkey等信息,格式如下:
{ "appkey":"11111", "appsecret":"2222", "token":"234345", "webid":"2234234", "unionid":"567567"}1)初始化
IJdClient client = null;private void init_JDClient(){ string url = "https://api.jd.com/routerjson"; this.client = new DefaultJdClient(url, dic["appkey"].ToString(), dic["appsecret"].ToString());}其中dic是一個Dictionary類型,保存了appkey等配置信息。
2)獲取商品基本信息接口調用
private string request_goodsInfo(){ ServicePromotionGoodsInfoRequest req = new ServicePromotionGoodsInfoRequest(); req.skuIds = txtGoodsID.Text;//商品ID值 ServicePromotionGoodsInfoResponse response = client.Execute(req, dic["token"], DateTime.Now.ToLocalTime()); return response.Body;}其中dic[‘token']是讀取字典中的token值,skuIds屬性是商品的ID值,這里Demo中用TextBox輸入。
3)獲取商品返現鏈接的接口調用
private string request_goodsRateUrl(){ ServicePromotionGetcodeRequest req = new ServicePromotionGetcodeRequest(); req.promotionType = 7; req.materialId = "http://item.jd.com/"+txtGoodsID.Text+".html";//注意,這里是商品的落地頁面,即實際鏈接 req.unionId = long.Parse(dic["unionid"].ToString());//聯盟ID req.channel = "PC";//PC電腦端,如果是手機端就是WL req.webId = dic["webid"].ToString();//網站ID //req.extendId = "jingdong"; //req.ext1 = "jingdong"; //req.adttype = "6"; //req.protocol = 0;//1為https,其他為http //req.pid = "jingdong"; ServicePromotionGetcodeResponse response = client.Execute(req, dic["token"], DateTime.Now.ToLocalTime()); return response.Body;}其中的materialID、unionId、webId是需要修改的,materialId是商品的實際頁面。
4)解析返回的數據
返回的數據是json格式的,所以需要引入C# JSON庫: Newtonsoft.Json
處理商品返現地址:
string urlinfo = request_goodsRateUrl();string url = "";JObject obj = JObject.Parse(urlinfo);string queryjs_result = (string)obj["jingdong_service_promotion_getcode_responce"]["queryjs_result"];obj = JObject.Parse(queryjs_result);if ((int)obj["resultCode"] == 0){ url = (string)obj["url"]; MessageBox.Show("返現地址:"+url);}處理商品基本信息:
string goodsinfo = request_goodsInfo();JObject obj = JObject.Parse(goodsinfo);string getpromotioninfo_result = (string)obj["jingdong_service_promotion_goodsInfo_responce"]["getpromotioninfo_result"];obj = JObject.Parse(getpromotioninfo_result);if ((bool)obj["sucessed"]){ obj = (JObject)obj["result"][0]; dataGridView1.Rows.Add(new object[] { "商品名稱", (string)obj["goodsName"] }); dataGridView1.Rows.Add(new object[] { "商品編號", (string)obj["skuId"] }); dataGridView1.Rows.Add(new object[] { "PC比率", (string)obj["commisionRatioPc"]+"%" }); dataGridView1.Rows.Add(new object[] { "WL比率", (string)obj["commisionRatioWl"]+"%" }); dataGridView1.Rows.Add(new object[] { "PC價格", "¥"+(string)obj["unitPrice"] }); dataGridView1.Rows.Add(new object[] { "WL價格", "¥"+(string)obj["wlUnitPrice"] }); WebRequest webreq = WebRequest.Create((string)obj["imgUrl"]); WebResponse webres = webreq.GetResponse(); using(Stream stream = webres.GetResponseStream()) { pictureBox1.Image = Image.FromStream(stream); pictureBox1.Tag = url; }}這里使用DataGridView顯示商品基本信息,圖片使用PictureBox顯示。
5)Demo預覽

6)文件下載
新聞熱點
疑難解答