国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

京東聯盟C#接口測試示例分享

2020-01-24 00:22:01
字體:
來源:轉載
供稿:網友

京東聯盟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)文件下載

jos-sdk-net-include-demo

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁乡县| 洪雅县| 沙坪坝区| 万源市| 嘉鱼县| 舟山市| 宁津县| 常熟市| 恭城| 留坝县| 亳州市| 凤阳县| 临城县| 腾冲县| 永修县| 曲靖市| 克拉玛依市| 静乐县| 安溪县| 永春县| 黎城县| 乌什县| 佛冈县| 比如县| 嘉荫县| 东山县| 康定县| 虞城县| 连平县| 资中县| 孝感市| 灵丘县| 宾川县| 合肥市| 南京市| 新绛县| 泰兴市| 仁怀市| 拉萨市| 电白县| 滨州市|