当前位置: 移动技术网 > IT编程>开发语言>c# > 京东联盟C#接口测试示例分享

京东联盟C#接口测试示例分享

2019年07月18日  | 移动技术网IT编程  | 我要评论

京东联盟c#接口的下载地址为:

下载后,默认是一个控制台程序,核心库和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)文件下载

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网