C#快递单号查询源码

源码本人测试过,没有啥问题,能查询快递单号,支持的快递还挺多,圆通快递、申通快递、韵达快递的都支持单号查询的,程序是通过向爱快递(www.aikuaidi.cn)接口传输参数来查询快递单号,我直接把代码帖出来,很好的解决我单个开发的麻烦。

/// <summary>

        /// 同步单号查询方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <param name="order"></param>
        /// <param name="isSign"></param>
        /// <param name="isLast"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static T APIQueryDataSYNC<T>(string id,
                                             string order,
                                             bool isSign,
                                             bool isLast,
                                             T defaultValue)
        {
            try
            {
                string currTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string currKey = key;
                if (isSign)
                {
                    currKey = Utils.GetSign(uid, key, id, order, currTime);
                    currKey += "&issign=true";
                }
 
                string url = sync_url + string.Format("?uid={0}&key={1}&id={2}&order={3}&time={4}",
                                            uid, currKey, id, order, HttpUtility.UrlEncode(currTime));
 
                string html = Utils.GET_WebRequestHTML("utf-8", url);
 
                if (!string.IsNullOrEmpty(html))
                    return Utils.JsonToObj<T>(html, defaultValue);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
 
            return defaultValue;
        }
 
    }
 
    /// <summary>
    /// 辅助工具类
    /// </summary>
    public class Utils
    {
        public static string GetSign(int uid, string key, string id, string order, string time)
        {
            string sign = string.Format("uid={0}&key={1}&id={2}&order={3}&time={4}",
                                        uid,
                                        key,
                                        id,
                                        HttpUtility.UrlEncode(order.ToLower()),
                                        HttpUtility.UrlEncode(time));
 
            return Md5Encrypt(sign.ToLower(), "utf-8");
        }
 
        public static string Md5Encrypt(string strToBeEncrypt, string encodingName)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            Byte[] FromData = System.Text.Encoding.GetEncoding(encodingName).GetBytes(strToBeEncrypt);
            Byte[] TargetData = md5.ComputeHash(FromData);
            string Byte2String = "";
            for (int i = 0; i < TargetData.Length; i++)
            {
                Byte2String += TargetData[i].ToString("x2");
            }
            return Byte2String;
        }
 
        public static T GetRequest<T>(string key, T defaultValue)
        {
            string value = HttpContext.Current.Request[key];
 
            if (string.IsNullOrEmpty(value))
            {
                return defaultValue;
            }
            else
            {
                try
                {
                    return (T)Convert.ChangeType(value, typeof(T));
                }
                catch
                {
                    return defaultValue;
                }
            }
        }
 
        public static T GetAppConfig<T>(string key, T defaultValue)
        {
            string value = ConfigurationManager.AppSettings[key];
 
            if (string.IsNullOrEmpty(value))
            {
                return defaultValue;
            }
            else
            {
                try
                {
                    return (T)Convert.ChangeType(value, typeof(T));
                }
                catch
                {
                    return defaultValue;
                }
            }
        }
 
        public static string ObjToJson<T>(T data)
        {
            try
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(data.GetType());
                using (MemoryStream ms = new MemoryStream())
                {
                    serializer.WriteObject(ms, data);
                    return Encoding.UTF8.GetString(ms.ToArray());
                }
            }
            catch
            {
                return null;
            }
        }
 
        public static T JsonToObj<T>(string json, T defaultValue)
        {
            try
            {
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
                {
                    object obj = serializer.ReadObject(ms);
 
                    return (T)Convert.ChangeType(obj, typeof(T));
                }
            }
            catch
            {
                return defaultValue;
            }
        }
 
        public static T XmlToObj<T>(string xml, T defaultValue)
        {
            try
            {
                System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
                {
                    object obj = serializer.ReadObject(ms);
 
                    return (T)Convert.ChangeType(obj, typeof(T));
                }
            }
            catch
            {
                return defaultValue;
            }
        }
 
        public static string ObjToXml<T>(T data)
        {
            System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, data);
                return Encoding.UTF8.GetString(ms.ToArray());
 
            }
        }
 
        public static string GET_WebRequestHTML(string encodingName, string htmlUrl)
        {
            string html = string.Empty;
 
            try
            {
                Encoding encoding = Encoding.GetEncoding(encodingName);
 
                WebRequest webRequest = WebRequest.Create(htmlUrl);
                HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, encoding);
 
                html = streamReader.ReadToEnd();
 
                httpWebResponse.Close();
                responseStream.Close();
                streamReader.Close();
            }
            catch (WebException ex)
            {
                throw new Exception(ex.Message);
            }
 
            return html;
        }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值