C#-【快递100】接口物流信息流程(二)

版权声明: https://blog.csdn.net/liuchang19950703/article/details/81911806

后台配置接入快递100申请:

附上快递100接口API统一查询物流单文档:

api.kuaidi100.com 获得物流单号的跟踪信息(免费)

  • 简介:通过向指定的地址发送请求,即可返回如JSON、XML等格式的跟综结果

1、应用场景

(1)电商网站用户打开“我的订单”时调用此API显示结果

(2)物流系统对帐前调用此API查一次所有运单的签收状态

2、是否需要授权

是,请到 快递查询API申请地址 申请

3、请求地址

http://api.kuaidi100.com/api?id=[]&com=[]&nu=[]&valicode=[]&show=[0|1|2|3]&muti=[0|1]&order=[desc|asc]

(使用时请先将上述地址中的 中括号 替换成下面相应的值)

4.输入参数

名称 类型 是否必需   描述
id String 身份授权key,请 快递查询接口 进行申请(大小写敏感)
com String 要查询的快递公司代码,不支持中文,对应的公司代码见 
API URL 所支持的快递公司及参数说明》和《支持的国际类快递及参数说明》。 
如果找不到您所需的公司,请发邮件至 [email protected]咨询(大小写不敏感)
nu String 要查询的快递单号,请勿带特殊符号,不支持中文(大小写不敏感)
valicode String 已弃用字段,无意义,请忽略。
show String 返回类型: 
0:返回json字符串, 
1:返回xml对象, 
2:返回html对象, 
3:返回text文本。 
如果不填,默认返回json字符串。
muti String 返回信息数量: 
1:返回多行完整的信息, 
0:只返回一行信息。 
不填默认返回多行。 
order String 排序: 
desc:按时间由新到旧排列, 
asc:按时间由旧到新排列。 
不填默认返回倒序(大小写不敏感)

5.返回结果

字段名称   字段含义
com 物流公司编号
nu 物流单号
time 每条跟踪信息的时间
context 每条跟综信息的描述
state 快递单当前的状态 :  
0:在途,即货物处于运输过程中;
1:揽件,货物已由快递公司揽收并且产生了第一条跟踪信息;
2:疑难,货物寄送过程出了问题;
3:签收,收件人已签收;
4:退签,即货物由于用户拒签、超区等原因退回,而且发件人已经签收;
5:派件,即快递正在进行同城派件;
6:退回,货物正处于退回发件人的途中;
该状态还在不断完善中,若您有更多的参数需求,欢迎发邮件至 [email protected] 提出。
status 查询结果状态: 
0:物流单暂无结果, 
1:查询成功, 
2:接口出现异常,
message 无意义,请忽略
condition 无意义,请忽略
ischeck 无意义,请忽略

6.返回示例

XML格式

    <xml>
    <message>ok</message>
    <nu>1200722815552</nu>
    <ischeck>1</ischeck>
    <com>yunda</com>
    <status>1</status>
    <condition>F00</condition>
    <data>
    <time>2013-03-03 19:24:48</time>
    <context>江苏泗阳县公司:进行揽件扫描</context>
    </data>
    <data>
    <time>2013-03-03 19:25:10</time>
    <context>江苏泗阳县公司:进行发出扫描,将发往:江苏淮安中转站</context>
    </data>
    <data>
    <time>2013-03-03 21:44:47</time>
    <context>江苏淮安中转站:快件进入分拨中心进行分拨</context>
    </data>
    <data>
    <time>2013-03-04 03:22:44</time>
    <context>江苏南京中转站:从站点发出,本次转运目的地:江苏南京栖霞区仙林公司</context>
    </data>
    <data>
    <time>2013-03-04 08:25:03</time>
    <context>江苏南京栖霞区仙林公司:到达目的地网点,快件将很快进行派送</context>
    </data>
    <data>
    <time>2013-03-04 13:09:58</time>
    <context>江苏南京栖霞区仙林公司:进行派件扫描;派送业务员:孙;(</context>
    </data>
    <data>
    <time>2013-03-04 13:19:47</time>
    <context>江苏南京栖霞区仙林公司:快件已被 图片 签收</context>
    </data>
    <state>3</state>
    </xml>
          


JSON格式

{
"message":"ok","status":"1","state":"3","data":[
  {"time":"2012-07-07 13:35:14","context":"客户已签收"},
  {"time":"2012-07-07 09:10:10","context":"离开[北京石景山营业厅]派送中,递送员[温],电话[]"},
  {"time":"2012-07-06 19:46:38","context":"到达[北京石景山营业厅]"},
  {"time":"2012-07-06 15:22:32","context":"离开[北京石景山营业厅]派送中,递送员[温],电话[]"},
  {"time":"2012-07-06 15:05:00","context":"到达[北京石景山营业厅]"},
  {"time":"2012-07-06 13:37:52","context":"离开[北京_同城中转站]发往[北京石景山营业厅]"},
  {"time":"2012-07-06 12:54:41","context":"到达[北京_同城中转站]"},
  {"time":"2012-07-06 11:11:03","context":"离开[北京运转中心驻站班组] 发往[北京_同城中转站]"},
  {"time":"2012-07-06 10:43:21","context":"到达[北京运转中心驻站班组]"},
  {"time":"2012-07-05 21:18:53","context":"离开[福建_厦门支公司] 发往 [北京运转中心_航空]"},
  {"time":"2012-07-05 20:07:27","context":"已取件,到达 [福建_厦门支公司]"}
]}
         

7. API工具

>> 快递100查询范例ASP版(38.2KB) 下载
>> 快递100查询范例PHP版(43.7KB) 下载
>> 快递100查询范例.NET版(38.6KB) 下载
>> 快递100查询范例JAVA版(8.40KB) 下载
>> Shopex、Ecshop、Hishop和最土团购等快递查询插件 下载

一.后台代码部分:

1.Express100Config类:根据物流公司和运单查询订单信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhClass;
using System.IO;
using System.Web;
using System.Net;
using Newtonsoft.Json;
namespace CS
{

    //返回参数:
    //com :物流公司单号
    //nu:物流单号
    //time:	每条跟踪信息的时间
    //context:每条跟综信息的描述
    //state:
    //快递单当前的状态 :  
    //0:在途,即货物处于运输过程中;
    //1:揽件,货物已由快递公司揽收并且产生了第一条跟踪信息;
    //2:疑难,货物寄送过程出了问题;
    //3:签收,收件人已签收;
    //4:退签,即货物由于用户拒签、超区等原因退回,而且发件人已经签收;
    //5:派件,即快递正在进行同城派件;
    //6:退回,货物正处于退回发件人的途中;
    //status:
    //查询结果状态: 
    //0:物流单暂无结果, 
    //1:查询成功, 
    //2:接口出现异常

    //返回Json格式:
    // {
    //"message":"ok","status":"1","state":"3","data":[
    //  {"time":"2012-07-07 13:35:14","context":"客户已签收"},
    //  {"time":"2012-07-07 09:10:10","context":"离开[北京石景山营业厅]派送中,递送员[温],电话[]"},
    //  {"time":"2012-07-06 19:46:38","context":"到达[北京石景山营业厅]"},
    //  {"time":"2012-07-06 15:22:32","context":"离开[北京石景山营业厅]派送中,递送员[温],电话[]"},
    //  {"time":"2012-07-06 15:05:00","context":"到达[北京石景山营业厅]"},
    //  {"time":"2012-07-06 13:37:52","context":"离开[北京_同城中转站]发往[北京石景山营业厅]"},
    //  {"time":"2012-07-06 12:54:41","context":"到达[北京_同城中转站]"},
    //  {"time":"2012-07-06 11:11:03","context":"离开[北京运转中心驻站班组] 发往[北京_同城中转站]"},
    //  {"time":"2012-07-06 10:43:21","context":"到达[北京运转中心驻站班组]"},
    //  {"time":"2012-07-05 21:18:53","context":"离开[福建_厦门支公司] 发往 [北京运转中心_航空]"},
    //  {"time":"2012-07-05 20:07:27","context":"已取件,到达 [福建_厦门支公司]"}
    //]}
    public class Express100Config
    {
        /// <summary>
        /// 根据物流公司和运单查询订单信息
        /// </summary>
        /// <param name="expressCompanyId">物流公司id</param>
        /// <param name="expressNum">物流单号</param>
        /// <returns></returns>
        public ExcuteResult GetExpressMessage(string expressCompanyId, string expressNum)
        {
            ExcuteResult result = new ExcuteResult();
            try
            {
                //输入参数:
                string id_key = "8014458806*****"; //身份授权key,请 快递查询接口 进行申请(大小写敏感)
                string com_company = expressCompanyId;//要查询的快递公司代码
                string nu_express_num = expressNum; //要查询的快递单号,请勿带特殊符号,不支持中文(大小写不敏感)
                string show_return_type = "0";//返回类型:0:返回json字符串,1:返回xml对象,2:返回html对象,3:返回text文本。如果不填,默认返回json字符串  
                string muti_return_is_muti = "1"; //返回信息数量: 1:返回多行完整的信息,0:只返回一行信息。 不填默认返回多行。 
                string order = "desc";  //排序desc:按时间由新到旧 ,asc:按时间由旧到新

                /****************  暂时测试 ****************/
                #region 正式
                //请求路径
                string apiurl = "http://api.kuaidi100.com/api?id=" + id_key + "&com=" + com_company + "&nu=" + nu_express_num + "&show=" + show_return_type + "&muti=" + muti_return_is_muti + "&order=" + order + "";
                WebRequest request = WebRequest.Create(@apiurl);
                WebResponse response = request.GetResponse();
                Stream stream = response.GetResponseStream();
                Encoding encode = Encoding.UTF8;
                StreamReader reader = new StreamReader(stream, encode);
                string detail = reader.ReadToEnd().Trim();
                #endregion
               
                if (detail.Length == 0)
                {
                    result.Result = false;
                    result.Msg = "快递返回的信息长度为空!";
                    ZH.SaveErr(new ErrorLog()
                    {
                        ErrorId = expressNum,
                        ErrorUser = Config.GardenUser == null ? "" : Config.GardenUser.uname,
                        ErrorTime = DateTime.Now,
                        ErrorClass = "Express100Config.cs",
                        ErrorMethod = "GetExpressMessage",
                        ErrorMsg = result.Msg
                    }.toJson());
                    return result;
                }
                Express100Return expressObject = JsonConvert.DeserializeObject<Express100Return>(detail);
                if (expressObject == null)
                {
                    result.Result = false;
                    result.Msg = "快递信息反序列失败!";
                    ZH.SaveErr(new ErrorLog()
                    {
                        ErrorId = expressNum,
                        ErrorUser = Config.GardenUser == null ? "" : Config.GardenUser.uname,
                        ErrorTime = DateTime.Now,
                        ErrorClass = "Express100Config.cs",
                        ErrorMethod = "GetExpressMessage",
                        ErrorMsg = result.Msg
                    }.toJson());
                    return result;
                }
                string message = expressObject.message.toString().Trim();
                string status = expressObject.status.toString().Trim();//0:物流单暂无结果 1:查询成功  2:接口出现异常 
                if (status == "0")
                {
                    result.Result = false;
                    result.Msg = "物流单暂无结果!";
                    ZH.SaveErr(new ErrorLog()
                    {
                        ErrorId = expressNum,
                        ErrorUser = Config.GardenUser == null ? "" : Config.GardenUser.uname,
                        ErrorTime = DateTime.Now,
                        ErrorClass = "Express100Config.cs",
                        ErrorMethod = "GetExpressMessage",
                        ErrorMsg = result.Msg
                    }.toJson());
                    return result;
                }
                else if (status == "2")
                {
                    result.Result = false;
                    result.Msg = "接口出现异常!";
                    ZH.SaveErr(new ErrorLog()
                    {
                        ErrorId = expressNum,
                        ErrorUser = Config.GardenUser == null ? "" : Config.GardenUser.uname,
                        ErrorTime = DateTime.Now,
                        ErrorClass = "Express100Config.cs",
                        ErrorMethod = "GetExpressMessage",
                        ErrorMsg = result.Msg
                    }.toJson());
                    return result;
                }
                result.Result = true;
                result.Msg = detail;
                ZH.SaveErr(new ErrorLog()
                {
                    ErrorId = expressNum,
                    ErrorUser = Config.GardenUser == null ? "" : Config.GardenUser.uname,
                    ErrorTime = DateTime.Now,
                    ErrorClass = "Express100Config.cs",
                    ErrorMethod = "GetExpressMessage",
                    ErrorMsg = result.Msg
                }.toJson());
                return result;
            }
            catch (Exception ex)
            {
                result.Result = false;
                result.Msg = ex.toString();
                ZH.SaveErr(new ErrorLog()
                {
                    ErrorId = expressNum,
                    ErrorUser = Config.GardenUser == null ? "" : Config.GardenUser.uname,
                    ErrorTime = DateTime.Now,
                    ErrorClass = "Express100Config.cs",
                    ErrorMethod = "GetExpressMessage",
                    ErrorMsg = result.Msg
                }.toJson());
                return result;
            }
        }
    }
}

2.Express100Return类:自定义用来填充参数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CS
{
    public class Express100Return
    {
        public string message { get; set; }
        public string status { get; set; }
        public string state { get; set; }
        public List<Express100ReturnData> data { get; set; }

    }
}

3.Express100ReturnData类,自定义类用来填充参数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CS
{
    public class Express100ReturnData
    {
        public string time { get; set; }
        public string context { get; set; }
    }
}

猜你喜欢

转载自blog.csdn.net/liuchang19950703/article/details/81911806