阿里云查询物流API

​​查询物流帮助类

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net.Http;
public static class ExpressHelper
{
        //使用API接口需要的配置参数
        private static string AppCode = "";
        private static string AppKey = "";
        private static string AppSecret = "";

        /// 
        /// 阿里云查询快递接口
        /// 
        /// 快递号
        /// SF=顺丰 ANE=安能物流
        /// 
        /// 
        public static string Search(string no, string type, out string errorMsg)
        {
            var url = string.Format("https://cexpress.market.alicloudapi.com/cexpress?no={0}&type={1}", no, type);
            string result = errorMsg = "";
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Add("Authorization", "APPCODE " + AppCode);
                    result = client.GetStringAsync(url).Result.ToString();

                    JObject obj = JsonConvert.DeserializeObject(result);
                    switch (obj["code"].ToString())
                    {
                        case "OK":
                            result = obj["list"].ToString();
                            break;
                        case "-1":
                            result = "";
                            errorMsg = "快递单号错误";
                            break;
                        case "203":
                            result = "";
                            errorMsg = "快递公司不存在";
                            break;
                        case "204":
                            result = "";
                            errorMsg = "快递公司识别失败";
                            break;
                        case "205":
                            result = "";
                            errorMsg = "暂无轨迹信息";
                            break;
                        case "207":
                            result = "";
                            errorMsg = "该单号被限制,错误单号";
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                result = "";
                errorMsg = ex.Message;
            }
            return result;
        }
}

返回结果即是物流查询数据,需要注意,在阿里云配置AppCode 以及 AppKey等参数

 

使用PostMan 测试结果如下

阿里云查询物流API_第1张图片

你可能感兴趣的:(物流接口,c#,后端,asp.net,阿里云)