unity查询快递物流信息

最近用到,研究记录下。

这里用到的是快递100。

1,先从快递100官网申请个账号,有基础版和企业版,基础版有限制,可查询100单/天,企业版收费。根据需求自己选择吧。

 

官网链接:https://api.kuaidi100.com

 

2,申请完毕,得到授权KEY。

 

 

3,官网API:http://www.kuaidi100.com/openapi/api_post.shtml
 

 

 

4,API示例代码:

 

5,仔细看官网示例代码,将代码复制出来,放入Unity。
官网代码:

    string url = "http://poll.kuaidi100.com/poll/query.do";
    Encoding encoding = Encoding.GetEncoding("utf-8");
 
    //参数
    String str = "******";
    String param = "{\"com\":\"******\",\"num\":\"" + str + "\",\"from\":\"\",\"to\":\"\"}";
    String customer = "******";
    String key = "******";
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    byte[] InBytes = Encoding.GetEncoding("UTF-8").GetBytes(param + key + customer);
    byte[] OutBytes = md5.ComputeHash(InBytes);
    string OutString = "";
    for (int i = 0; i < OutBytes.Length; i++)
    {
        OutString += OutBytes[i].ToString("x2");
    }
    String sign = OutString.ToUpper();
    IDictionary parameters = new Dictionary();
    parameters.Add("param", param);
    parameters.Add("customer", customer);
    parameters.Add("sign", sign);
    HttpWebResponse response = Program.CreatePostHttpResponse(url, parameters, encoding);
    //打印返回值
    Stream stream = response.GetResponseStream();   //获取响应的字符串流
    StreamReader sr = new StreamReader(stream); //创建一个stream读取流
    string html = sr.ReadToEnd();   //从头读到尾,放到字符串html
    Console.WriteLine(html);

6,看完官方文档,脑袋里就只剩一首歌:小朋友,你是否有很多问号???发现无论哪个官网的代码都是个坑啊,不是这错就是那错,关键还少个函数,这尼玛我用个鸡儿。搞搞搞半天,对比文档,发现有几个参数是不必须的,这里就把代码修改、补充了下。

public string Code;//快递公司编码(在下面文档)
public string ExpressNumber;//快递编号   
public string customer;//公司编号,申请得到的KEY里面有,替换成自己的customer
public string key;//KEY,申请得到的KEY里面有,替换成自己的key
 
void init()
{
    string url = "http://poll.kuaidi100.com/poll/query.do";
    Encoding encoding = Encoding.GetEncoding("utf-8");
    //参数
    string param = "{\"com\":\"" + Code + "\",\"num\":\"" + ExpressNumber + "\",\"phone\":\"\",\"from\":\"\",\"to\":\"\"}";
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    byte[] InBytes = Encoding.GetEncoding("UTF-8").GetBytes(param + key + customer);
    byte[] OutBytes = md5.ComputeHash(InBytes);
    string OutString = "";
    for (int i = 0; i < OutBytes.Length; i++)
    {
        OutString += OutBytes[i].ToString("x2");
    }
    string sign = OutString.ToUpper();
    Debug.Log(sign);
    Dictionary<string, string> parameters = new Dictionary<string, string>();
    parameters.Add("param", param);
    parameters.Add("customer", customer);
    parameters.Add("sign", sign);
    HttpWebResponse response = (HttpWebResponse)CreatePostHttpResponse(url, parameters, encoding).GetResponse();
    //打印返回值
    Stream stream = response.GetResponseStream();   //获取响应的字符串流
    StreamReader sr = new StreamReader(stream); //创建一个stream读取流
    string html = sr.ReadToEnd();   //从头读到尾,放到字符串html
    //Console.WriteLine(html);
    Debug.Log(html);
}
 
//官网没有的补上
public HttpWebRequest CreatePostHttpResponse(string url, Dictionary<string, string> param, Encoding encoding)
{
    StringBuilder postData = new StringBuilder();
    if (param != null && param.Count > 0)
    {
        foreach (var p in param)
        {
            if (postData.Length > 0)
            {
                postData.Append("&");
            }
            postData.Append(p.Key);
            postData.Append("=");
            postData.Append(p.Value);
        }
        Debug.Log(postData);
    }
 
    byte[] byteData = encoding.GetBytes(postData.ToString());
    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.ContentType = "application/x-www-form-urlencoded";
        request.Referer = url;
        request.Accept = "*/*";
        request.Timeout = 30 * 1000;
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
        request.Method = "POST";
        request.ContentLength = byteData.Length;
        Stream stream = request.GetRequestStream();
        stream.Write(byteData, 0, byteData.Length);
        stream.Flush();
        stream.Close();
        return request;
    }
    catch (Exception)
    {
 
        throw;
    }
 
 
}

7,运行结果:

 

 

没有问题,此时信息已得到,json数据,解析就可以了。

注:技术不断更新,请随时关注官方文档。


源码,需要的朋友可以下载下。

链接:unity物流快递信息查询

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值