Advertisement

unity查询快递物流信息

阅读量:

**最近用到,研究记录下。

这里用到的是快递100。

为了方便进行订单管理及数据分析的需求,在快递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);
    
    
    
    
    cs
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/QjGd83vXgYDcJbxkzMANOnwPTsS6.png)

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;
    
     }
    
  
    
  
    
 }
    
    
    
    
    cs
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-18/7de9AByir2PUgSaXjODH6c1s48Tu.png)

7,结果:

**

**

无此问题,则当前已获取相关信息;即可完成JSON数据的解析工作。

注:技术不断更新,请随时关注官方文档。** 源码,需要的朋友可以下载下。

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

全部评论 (0)

还没有任何评论哟~