快递物流查询API接口如何用C#调用
快递查询过程中积累的大量数据,对于企业的运营决策具有重要价值。通过对这些数据的分析,企业可以了解物流运输的趋势、客户的需求偏好等,进而优化物流配送策略、调整库存管理、改进产品供应等,提升企业的整体竞争力和盈利能力。
且随着企业业务的发展和市场需求的变化,快递物流查询API接口也可以为企业的业务拓展和创新提供支持。例如,企业可以基于快递物流数据开发增值服务,如物流保险、快递代收代发等业务,拓展收入来源。
以下是用C#调用快递物流查询API接口的代码示例:
using System;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
public class ApiDemo {
private const String url = "https://api.shuxuntech.com/v1/express/query";
private const String method = "POST";
private const String appKey = "您的appKey";
private const String appSecret = "您的appSecret";
public static void Main(string[] args) {
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
String timestamp = Convert.ToInt64(ts.TotalMilliseconds).ToString();
String sign = SHA256(appKey + timestamp + appSecret);
HttpWebRequest httpRequest = null;
HttpWebResponse httpResponse = null;
httpRequest = (HttpWebRequest) WebRequest.CreateDefault(new Uri(url));
httpRequest.Method = method;
httpRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
httpRequest.Headers.Add("appKey", appKey);
httpRequest.Headers.Add("timestamp", timestamp);
httpRequest.Headers.Add("sign", sign);
String no = "";
String code = "";
String tel = "";
String params = "no=" + no + "&code=" + code + "&tel=" + tel;
byte[] data = Encoding.UTF8.GetBytes(params);
using (Stream stream = httpRequest.GetRequestStream()) {
stream.Write(data, 0, data.Length);
}
httpResponse = (HttpWebResponse) httpRequest.GetResponse();
Console.WriteLine(httpResponse.StatusCode);
Stream st = httpResponse.GetResponseStream();
StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
Console.WriteLine(reader.ReadToEnd());
}
public static string SHA256(string str) {
byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
SHA256Managed Sha256 = new SHA256Managed();
byte[] by = Sha256.ComputeHash(SHA256Data);
return BitConverter.ToString(by).Replace("-", "").ToLower();
}
}
返回的数据:
(1)正确返回
{
"code": "0",
"msg": "成功",
"isFee": 1,
"seqNo": "8bdguy0kbojrkq9cf5tp3l4tidbpile3",
"data": {
"theLastTime": "2024-02-28 13:47:43", //运单号物流流转当前最新描述
"cpCode": "EMS", //快递公司编码
"mailNo": "9858345266141", //快递单号
"cpMobile": "11183", //快递公司电话
"theLastMessage": "您的快件已签收【南阳理工学院软件学院东菜鸟驿站】,如有疑问请电联快递员【电话:15737715502】。连接美好,无处不在,感谢您使用中国邮政,期待再次为您服务。",//运单号物流流转当前最新描述
"logisticsCompanyName": "EMS", //快递公司名称
"cpUrl": "https://www.ems.com.cn/", //快递公司官网
"takeTime": "2天3小时50分", //从揽收到送达所耗时间
"logisticsStatusDesc": "已签收", //运单号当前物流状态文字描述
"logisticsTraceDetailList": [ //物流流转明细
{
"areaCode": "CN411300000000", //非必须,当前节点所在地址行政编码
"areaName": "河南省,南阳市", //非必须,当前节点所在地区,省,市,区或省,市
"subLogisticsStatus": "SIGN", //物流流转子状态,详见:物流状态编码对照表
"time": 1709099263000, //时间,单位毫秒
"logisticsStatus": "SIGN", //物流流转状态,详见:物流状态编码对照表
"desc": "您的快件已签收【南阳理工学院软件学院东菜鸟驿站】,如有疑问请电联快递员【电话:15737715502】。连接美好,无处不在,感谢您使用中国邮政,期待再次为您服务。"//物流流转描述
},
{
"areaCode": "CN411300000000",
"areaName": "河南省,南阳市",
"subLogisticsStatus": "STA_INBOUND",
"time": 1709098104000,
"logisticsStatus": "DELIVERING",
"desc": "您的快件已派送至【南阳理工学院软件学院东菜鸟驿站】,自提点电话:15303770902,请您凭取件码领取快件,如有疑问请电联快递员【电话:15737715502】,投诉请致电11183。"
},
{
"areaCode": "CN411300000000",
"areaName": "河南省,南阳市",
"subLogisticsStatus": "DELIVERING",
"time": 1709084792000,
"logisticsStatus": "DELIVERING",
"desc": "快件正在派送中,请耐心等待,保持电话畅通,准备签收,如有疑问请电联快递员【苗莹莹,电话:15737715502】或揽投部【电话:0377-63116200】,投诉请致电11183。"
},
{
"areaCode": "CN411300000000",
"areaName": "河南省,南阳市",
"subLogisticsStatus": "TRANSPORT",
"time": 1709084492000,
"logisticsStatus": "TRANSPORT",
"desc": "快件到达【南阳市白河揽投部】"
},
{
"areaCode": "CN411300000000",
"areaName": "河南省,南阳市",
"subLogisticsStatus": "TRANSPORT",
"time": 1709074003000,
"logisticsStatus": "TRANSPORT",
"desc": "快件离开【南阳市包件车间】,正在发往下一站"
},
{
"areaCode": "CN411300000000",
"areaName": "河南省,南阳市",
"subLogisticsStatus": "TRANSPORT",
"time": 1709058177000,
"logisticsStatus": "TRANSPORT",
"desc": "快件到达【南阳市包件车间】"
},
{
"areaCode": "CN411700000000",
"areaName": "河南省,驻马店市",
"subLogisticsStatus": "TRANSPORT",
"time": 1709036234000,
"logisticsStatus": "TRANSPORT",
"desc": "快件离开【驻马店市包件车间】,正在发往下一站"
},
{
"areaCode": "CN411700000000",
"areaName": "河南省,驻马店市",
"subLogisticsStatus": "TRANSPORT",
"time": 1709032670000,
"logisticsStatus": "TRANSPORT",
"desc": "快件到达【驻马店市包件车间】"
},
{
"areaCode": "CN510100000000",
"areaName": "四川省,成都市",
"subLogisticsStatus": "TRANSPORT",
"time": 1708962186000,
"logisticsStatus": "TRANSPORT",
"desc": "快件离开【成都双流处理中心天府包件车间】,正在发往下一站"
},
{
"areaCode": "CN510100000000",
"areaName": "四川省,成都市",
"subLogisticsStatus": "TRANSPORT",
"time": 1708952075000,
"logisticsStatus": "TRANSPORT",
"desc": "快件到达【成都双流处理中心天府包件车间】"
},
{
"areaName": "四川省,成都市,新津县",
"subLogisticsStatus": "TRANSPORT",
"time": 1708952067000,
"logisticsStatus": "TRANSPORT",
"desc": "快件离开【新津区电商客户平岗路揽投部】,正在发往【成都双流处理中心天府包件车间】"
},
{
"areaName": "四川省,成都市,新津县",
"subLogisticsStatus": "TRANSPORT",
"time": 1708952066000,
"logisticsStatus": "TRANSPORT",
"desc": "快件已在【新津区电商客户平岗路揽投部】完成分拣,准备发出"
},
{
"areaCode": "CN510100000000",
"areaName": "四川省,成都市",
"subLogisticsStatus": "ACCEPT",
"time": 1708911453000,
"logisticsStatus": "ACCEPT",
"desc": "中国邮政 已收取快件"
}
],
"logisticsStatus": "SIGN" //物流流转状态,详见:物流状态编码对照表;
}
}
(2)错误返回
{
"code": "1",
"msg": "请输入快递编号",
"isFee": 0,
"seqNo": null,
"data": null
}
不过需要注意的是,实际应用中你需要先获取合法的 API 密钥等相关配置信息,并根据具体使用的 API 提供商的文档来准确实现完整功能。