好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

京东联盟 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

原文链接:https://www.coderecord.cn/jd-union-net-demo.html

dy("nrwz");

查看更多关于京东联盟C#接口测试示例分享的详细内容...

  阅读:44次