今日咱们来聊怎么运用快递鸟供给的接口来查询【申通快递】的物流轨道。
首要咱们准备好需求用到的资源,
测验商户ID:
test1617571
测验API key:
554343b2-7252-439b-b4eb-1af42c8f2175(此Key仅限测验环境运用)
API测验地址:
http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json
我以C#开发为例来解说,
假如还没有注册快递鸟,或许还不知道怎么获取API Key,能够拜访这个腾讯视频教程:
https://v.qq.com/x/page/z3068yfw0fl.html(注册登陆获取快递鸟API Key)接下来,咱们解说一下恳求参数
接口参数,也叫事务参数,恳求的事务接口不同,参数的字段,内容也不同,是跟着事务改变的,这儿咱们完成的是即时查询接口,恳求指令类型:1002,快递鸟官网要求有必要传递快递公司编码和物流单号 ,圆通快递的编码是YTO
如要检查其他快递公司的编码,点击此链接:
http://www.kdniao.com/documents
正如我前面的轨道截图,申通快递的编码是STO,物流单号是777007389359024
事务参数报文组合如下:
{'OrderCode':'','ShipperCode':'STO','LogisticCode':'777007389359024'}
恳求的完好报文(URL编码今后):
RequestData=%0d%0a++++++++++++%7b%27OrderCode%27%3a%27%27%2c%27ShipperCode%27%3a%27STO%27%2c%27LogisticCode%27%3a%27777007389359024%27%7d%0d%0a++++++++&EBusinessID=1617571&RequestType=1002&DataSign=N2I4ZGEzYjkwMzhkZWY0YmM5ZGYxOGNkYjU5MmI2ZTU%3d&DataType=2
未编码的恳求报文:
RequestData=
{'OrderCode':'','ShipperCode':'STO','LogisticCode':'777007389359024'}
&EBusinessID=1617571&RequestType=1002&DataSign=N2I4ZGEzYjkwMzhkZWY0YmM5ZGYxOGNkYjU5MmI2ZTU=&DataType=2
回来的报文信息:
{ "LogisticCode": "777007389359024", "ShipperCode": "STO", "Traces": [ { "AcceptStation": "您的宝物即将由申通快递(江苏省市场部三十七部)全程护航,揽件员(宏峰路)。因为疫情防控和交通管制的影响,您的快件时效可能会受到影响,希望能得到您的了解,概况可咨询当地网点", "AcceptTime": "2020-01-08 19:45:01" }, { "AcceptStation": "快件已由【江苏射阳公司】扫描发往【江苏盐城中转部】,扫描员【孙霞】。因为疫情防控和交通管制的影响,您的快件时效可能会受到影响,请您耐性等候,感谢了解", "AcceptTime": "2020-01-09 15:28:36" }, { "AcceptStation": "快件已由【江苏射阳公司】扫描发往【江苏盐城中转部】,扫描员【射阳】。因为疫情防控和交通管制的影响,您的快件时效可能会受到影响,请您耐性等候,感谢了解", "AcceptTime": "2020-01-09 17:59:53" }, { "AcceptStation": "已抵达-江苏盐城中转部", "AcceptTime": "2020-01-09 19:57:22" }, { "AcceptStation": "快件已由【江苏盐城中转部】扫描发往【江苏江阴航空部】,扫描员【陈建群】。因为疫情防控和交通管制的影响,您的快件时效可能会受到影响,请您耐性等候,感谢了解", "AcceptTime": "2020-01-09 20:16:29" }, { "AcceptStation": "已抵达-江苏江阴航空部", "AcceptTime": "2020-01-10 02:28:24" }, { "AcceptStation": "快件已由【江苏江阴航空部】扫描发往【广东深圳转运中心】,扫描员【张华臣】。因为疫情防控和交通管制的影响,您的快件时效可能会受到影响,请您耐性等候,感谢了解", "AcceptTime": "2020-01-10 02:30:14" }, { "AcceptStation": "已抵达-广东深圳转运中心", "AcceptTime": "2020-01-11 00:48:28" }, { "AcceptStation": "快件已由【广东深圳转运中心】扫描发往【广东罗湖龙华横林分部】,扫描员【深圳进港自动化中心】。因为疫情防控和交通管制的影响,您的快件时效可能会受到影响,请您耐性等候,感谢了解", "AcceptTime": "2020-01-11 00:48:31" }, { "AcceptStation": "快件已由【广东深圳转运中心】扫描发往【广东罗湖龙华横林分部】,扫描员【深圳进港自动化中心】。因为疫情防控和交通管制的影响,您的快件时效可能会受到影响,请您耐性等候,感谢了解", "AcceptTime": "2020-01-11 00:49:01" }, { "AcceptStation": "已抵达-广东罗湖龙华横林分部", "AcceptTime": "2020-01-11 08:06:34" }, { "AcceptStation": "已抵达-广东罗湖龙华横林分部", "AcceptTime": "2020-01-11 10:33:36" }, { "AcceptStation": "疫情无情,人有情,申通小哥一直在据守。您的快件即将由(广东罗湖龙华横林分部)的派件员(王忠明)送达,为保证广大客户寄递安全,削减人员直接触摸,咱们主张您在取件时做好安全防护", "AcceptTime": "2020-01-11 11:18:36" }, { "AcceptStation": "快递已被【丰巢智能柜】深圳市龙华区鑫茂花园A区2栋1单元地下车库负1层门口丰巢快递柜代收,请及时取件。有问题请联络13925214311", "AcceptTime": "2020-01-11 12:55:57" }, { "AcceptStation": "已签收,收件人凭客户短信取件签收", "AcceptTime": "2020-01-11 22:18:09" } ], "State": "3", "EBusinessID": "1617571", "Success": true}C#要害代码:/// <summary> /// 即时查询物流轨道 /// </summary> /// <param name="url">http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json</param> /// <param name="eBusinessID">test1617571</param> /// <param name="appKey">554343b2-7252-439b-b4eb-1af42c8f2175</param> /// <param name="requestData">{'OrderCode':'','ShipperCode':'ZTO','LogisticCode':'78120038107849'}</param> /// <returns></returns> public string getOrderTracesByJson(string url,string eBusinessID, string appKey, string requestData) { Dictionary<string, string> param = new Dictionary<string, string>(); param.Add("RequestData", HttpUtility.UrlEncode(requestData, Encoding.UTF8)); param.Add("EBusinessID", eBusinessID); param.Add("RequestType", "1002"); string dataSign = encrypt(requestData, appKey, "UTF-8"); param.Add("DataSign", HttpUtility.UrlEncode(dataSign, Encoding.UTF8)); param.Add("DataType", "2"); string result = sendPost(url, param); return result; } /// <summary> /// Post方法提交数据,回来网页的源代码 /// </summary> /// <param name="url">发送恳求的 URL</param> /// <param name="param">恳求的参数调集</param> /// <returns>长途资源的呼应成果</returns> private string sendPost(string url, Dictionary<string, string> param) { string result = ""; StringBuilder postData = new StringBuilder(); if (param != null && param.Count > 0) { foreach (var p in param) { if (postData.Length > 0) { postData.Append("&"); } postData.Append(p.Key); postData.Append("="); postData.Append(p.Value); } } // return postData.ToString(); byte[] byteData = Encoding.GetEncoding("UTF-8").GetBytes(postData.ToString()); try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.ContentType = "application/x-www-form-urlencoded"; request.Referer = url; request.Accept = "*/*"; request.Timeout = 30 * 1000; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"; request.Method = "POST"; request.ContentLength = byteData.Length; Stream stream = request.GetRequestStream(); stream.Write(byteData, 0, byteData.Length); stream.Flush(); stream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream backStream = response.GetResponseStream(); StreamReader sr = new StreamReader(backStream, Encoding.GetEncoding("UTF-8")); result = sr.ReadToEnd(); sr.Close(); backStream.Close(); response.Close(); request.Abort(); } catch (Exception ex) { result = ex.Message; } return result; }///<summary> ///电商Sign签名 ///</summary> ///<param name="content">内容</param> ///<param name="keyValue">Appkey</param> ///<param name="charset">URL编码 </param> ///<returns>DataSign签名</returns> private string encrypt(String content, String keyValue, String charset) { if (keyValue != null) { return base64(MD5(content + keyValue, charset), charset); } return base64(MD5(content, charset), charset); } ///<summary> /// 字符串MD5加密 ///</summary> ///<param name="str">要加密的字符串</param> ///<param name="charset">编码方法</param> ///<returns>密文</returns> private string MD5(string str, string charset) { byte[] buffer = System.Text.Encoding.GetEncoding(charset).GetBytes(str); try { System.Security.Cryptography.MD5CryptoServiceProvider check; check = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] somme = check.ComputeHash(buffer); string ret = ""; foreach (byte a in somme) { if (a < 16) ret += "0" + a.ToString("X"); else ret += a.ToString("X"); } return ret.ToLower(); } catch { throw; } } /// <summary> /// base64编码 /// </summary> /// <param name="str">内容</param> /// <param name="charset">编码方法</param> /// <returns></returns> private string base64(String str, String charset) { return Convert.ToBase64String(System.Text.Encoding.GetEncoding(charset).GetBytes(str)); }