How to realize express doc No. identification - Express bird Express query API

From , 3 Years ago, written in C#, viewed 53 times.
URL https://pastebin.vip/view/220a7f49
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Web;
  8.  
  9. namespace KdGoldAPI
  10. {
  11.     public class KdApiOrderDistinguish
  12.     {
  13.         //电商ID
  14.         private string EBusinessID = "请到快递鸟官网申请http://www.kdniao.com/ServiceApply.aspx";
  15.         //电商加密私钥,快递鸟提供,注意保管,不要泄漏
  16.         private string AppKey = "请到快递鸟官网申请http://www.kdniao.com/ServiceApply.aspx";
  17.         //请求url
  18.         //测试环境
  19.         private string ReqURL = "http://testapi.kdniao.cc:8081/Ebusiness/EbusinessOrderHandle.aspx";
  20.         //正式环境
  21.         //private string ReqURL = "http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";
  22.  
  23.         /// <summary>
  24.         /// Json方式  单号识别
  25.         /// </summary>
  26.         /// <returns></returns>
  27.         public string orderTracesSubByJson()
  28.         {
  29.             string requestData = "{'LogisticCode': '3967950525457'}";
  30.  
  31.             Dictionary<string, string> param = new Dictionary<string, string>();
  32.             param.Add("RequestData", HttpUtility.UrlEncode(requestData, Encoding.UTF8));
  33.             param.Add("EBusinessID", EBusinessID);
  34.             param.Add("RequestType", "2002");
  35.             string dataSign = encrypt(requestData, AppKey, "UTF-8");
  36.             param.Add("DataSign", HttpUtility.UrlEncode(dataSign, Encoding.UTF8));
  37.             param.Add("DataType", "2");
  38.  
  39.             string result = sendPost(ReqURL, param);
  40.  
  41.             //根据公司业务处理返回的信息......
  42.  
  43.             return result;
  44.         }
  45.  
  46.         /// <summary>
  47.         /// Post方式提交数据,返回网页的源代码
  48.         /// </summary>
  49.         /// <param name="url">发送请求的 URL</param>
  50.         /// <param name="param">请求的参数集合</param>
  51.         /// <returns>远程资源的响应结果</returns>
  52.         private string sendPost(string url, Dictionary<string, string> param)
  53.         {
  54.             string result = "";
  55.             StringBuilder postData = new StringBuilder();
  56.             if (param != null && param.Count > 0)
  57.             {
  58.                 foreach (var p in param)
  59.                 {
  60.                     if (postData.Length > 0)
  61.                     {
  62.                         postData.Append("&");
  63.                     }
  64.                     postData.Append(p.Key);
  65.                     postData.Append("=");
  66.                     postData.Append(p.Value);
  67.                 }
  68.             }
  69.             byte[] byteData = Encoding.GetEncoding("UTF-8").GetBytes(postData.ToString());
  70.             try
  71.             {
  72.  
  73.                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  74.                 request.ContentType = "application/x-www-form-urlencoded";
  75.                 request.Referer = url;
  76.                 request.Accept = "*/*";
  77.                 request.Timeout = 30 * 1000;
  78.                 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)";
  79.                 request.Method = "POST";
  80.                 request.ContentLength = byteData.Length;
  81.                 Stream stream = request.GetRequestStream();
  82.                 stream.Write(byteData, 0, byteData.Length);
  83.                 stream.Flush();
  84.                 stream.Close();
  85.                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  86.                 Stream backStream = response.GetResponseStream();
  87.                 StreamReader sr = new StreamReader(backStream, Encoding.GetEncoding("UTF-8"));
  88.                 result = sr.ReadToEnd();
  89.                 sr.Close();
  90.                 backStream.Close();
  91.                 response.Close();
  92.                 request.Abort();
  93.             }
  94.             catch (Exception ex)
  95.             {
  96.                 result = ex.Message;
  97.             }
  98.             return result;
  99.         }
  100.  
  101.         ///<summary>
  102.         ///电商Sign签名
  103.         ///</summary>
  104.         ///<param name="content">内容</param>
  105.         ///<param name="keyValue">Appkey</param>
  106.         ///<param name="charset">URL编码 </param>
  107.         ///<returns>DataSign签名</returns>
  108.         private string encrypt(String content, String keyValue, String charset)
  109.         {
  110.             if (keyValue != null)
  111.             {
  112.                 return base64(MD5(content + keyValue, charset), charset);
  113.             }
  114.             return base64(MD5(content, charset), charset);
  115.         }
  116.  
  117.         ///<summary>
  118.         /// 字符串MD5加密
  119.         ///</summary>
  120.         ///<param name="str">要加密的字符串</param>
  121.         ///<param name="charset">编码方式</param>
  122.         ///<returns>密文</returns>
  123.         private string MD5(string str, string charset)
  124.         {
  125.             byte[] buffer = System.Text.Encoding.GetEncoding(charset).GetBytes(str);
  126.             try
  127.             {
  128.                 System.Security.Cryptography.MD5CryptoServiceProvider check;
  129.                 check = new System.Security.Cryptography.MD5CryptoServiceProvider();
  130.                 byte[] somme = check.ComputeHash(buffer);
  131.                 string ret = "";
  132.                 foreach (byte a in somme)
  133.                 {
  134.                     if (a < 16)
  135.                         ret += "0" + a.ToString("X");
  136.                     else
  137.                         ret += a.ToString("X");
  138.                 }
  139.                 return ret.ToLower();
  140.             }
  141.             catch
  142.             {
  143.                 throw;
  144.             }
  145.         }
  146.  
  147.         /// <summary>
  148.         /// base64编码
  149.         /// </summary>
  150.         /// <param name="str">内容</param>
  151.         /// <param name="charset">编码方式</param>
  152.         /// <returns></returns>
  153.         private string base64(String str, String charset)
  154.         {
  155.             return Convert.ToBase64String(System.Text.Encoding.GetEncoding(charset).GetBytes(str));
  156.         }
  157.     }
  158. }

Reply to "How to realize express doc No. identification - Express bird Express query API"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website