快递100-获取实时物流信息

  public class ExpressHelper
    {
        /// <summary>
        /// 快递100 获取数据接口
        /// </summary>
        /// <param name="customer">快递100分配的的公司编号</param>
        /// <param name="key">客户授权key</param>
        /// <param name="expressNo">快递单号</param>
        /// <param name="expressCode">快递Code</param>
        /// <returns></returns>
        public static string getExpressData(string customer, string key, string expressNo, string expressCode)
        {
            try
            {

                UTF8Encoding encoding = new UTF8Encoding();

                string param = synQueryData(expressCode, expressNo, "", "", "", 1);
                var sign = CommonHelper.Md5(param + key + customer);
                string postStrTpl = "customer={0}&sign={1}&param={2}";
                byte[] postData = encoding.GetBytes(string.Format(postStrTpl, customer, sign.ToUpper(), param));

                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://poll.kuaidi100.com/poll/query.do");
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = postData.Length;

                Stream newStream = myRequest.GetRequestStream();

                newStream.Write(postData, 0, postData.Length);
                newStream.Flush();
                newStream.Close();

                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                if (myResponse.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);

                    string res = reader.ReadToEnd();
                    return res;

                }
                else
                {
                    //访问失败
                    return "False";
                }
            }
            catch (Exception e)
            {

                return e.Message;
            }
        }


        #region 获取快递Code

        private static string getExpressCode(string key, string num)
        {
            string code = "";
            try
            {

                UTF8Encoding encoding = new UTF8Encoding();
                string postStrTpl = string.Format("http://www.kuaidi100.com/autonumber/auto?num={0}&key={1}", num, key);

                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(postStrTpl);
                myRequest.ContentType = "application/x-www-form-urlencoded";
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                if (myResponse.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);

                    string res = reader.ReadToEnd();
                    JObject jos = (JObject)JsonConvert.DeserializeObject(res);
                    code = jos["comCode"].ToString();
                }
            }
            catch (Exception e)
            {
                LogHelper.WriteLog("getExpressCode", e);
            }
            return code;

        }
        #endregion

        #region 实时查询快递单号

        /// <summary>
        ///  实时查询快递单号
        /// </summary>
        /// <param name="com">快递公司编码</param>
        /// <param name="num">快递单号</param>
        /// <param name="phone">手机号</param>
        /// <param name="from">出发地城市</param>
        /// <param name="to">目的地城市</param>
        /// <param name="resultv2">开通区域解析功能:0-关闭;1-开通</param>
        /// <returns></returns>
        private static string synQueryData(String com, String num, String phone, String from, String to, int resultv2)
        {

            StringBuilder param = new StringBuilder("{");
            param.Append("\"com\":\"").Append(com).Append("\"");
            param.Append(",\"num\":\"").Append(num).Append("\"");
            param.Append(",\"phone\":\"").Append(phone).Append("\"");
            param.Append(",\"from\":\"").Append(from).Append("\"");
            param.Append(",\"to\":\"").Append(to).Append("\"");
            if (1 == resultv2)
            {
                param.Append(",\"resultv2\":1");
            }
            else
            {
                param.Append(",\"resultv2\":0");
            }
            param.Append("}");

            return param.ToString();
        }
        #endregion
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

acycwf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值