[C#] 如何对接免费不限量的快递查询API接口 →→→→→进入此内容的聊天室

来自 , 2020-12-09, 写在 C#, 查看 122 次.
URL http://www.code666.cn/view/7c4bf50b
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. using System.Net;
  7. using System.IO;
  8.  
  9. namespace KdGoldAPI
  10. {
  11.     public class KdApiSubscribeDemo
  12.     {
  13.         //电商ID
  14.         private string EBusinessID = "1237100";
  15.         //电商加密私钥,快递鸟提供,注意保管,不要泄漏(需自行申请)
  16.         private string AppKey = "518a73d8-1f7f-441a-b644-33e77b49d846";
  17.         //请求url
  18.         private string ReqURL = "http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";
  19.  
  20.         /// <summary>
  21.         /// Json方式  物流信息订阅
  22.         /// </summary>
  23.         /// <returns></returns>
  24.         public string orderTracesSubByJson()
  25.         {
  26.             string requestData = "{'Code': 'SF','Item': [" +
  27.                                "{'No': '909261024507','Bk': 'test'}," +
  28.                                "{'No': '589554393102','Bk': 'test'}," +
  29.                                "{'No': '589522101958','Bk': 'test'}," +
  30.                                "{'No': '909198822942', 'Bk': 'test'}" +
  31.                                "]}";
  32.  
  33.             Dictionary<string, string> param = new Dictionary<string, string>();
  34.             param.Add("RequestData", HttpUtility.UrlEncode(requestData, Encoding.UTF8));
  35.             param.Add("EBusinessID", EBusinessID);
  36.             param.Add("RequestType", "1005");
  37.             string dataSign = encrypt(requestData, AppKey, "UTF-8");
  38.             param.Add("DataSign", HttpUtility.UrlEncode(dataSign, Encoding.UTF8));
  39.             param.Add("DataType", "2");
  40.  
  41.             string result = sendPost(ReqURL, param);
  42.  
  43.             //根据公司业务处理返回的信息......
  44.  
  45.             return result;
  46.         }
  47.  
  48.         /// <summary>
  49.         ///  XML方式  物流信息订阅
  50.         /// </summary>
  51.         /// <returns></returns>
  52.         public string orderTracesSubByXml()
  53.         {
  54.             string requestData = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
  55.                                 "<Content>" +
  56.                                 "<Code>SF</Code>" +
  57.                                 "<Items>" +
  58.                                 "<Item>" +
  59.                                 "<No>909261024507</No>" +
  60.                                 "<Bk>test</Bk>" +
  61.                                 "</Item>" +
  62.                                 "<Item>" +
  63.                                 "<No>909261024507</No>" +
  64.                                 "<Bk>test</Bk>" +
  65.                                 "</Item>" +
  66.                                 "</Items>" +
  67.                                 "</Content>";
  68.  
  69.             Dictionary<string, string> param = new Dictionary<string, string>();
  70.             param.Add("RequestData", HttpUtility.UrlEncode(requestData, Encoding.UTF8));
  71.             param.Add("EBusinessID", EBusinessID);
  72.             param.Add("RequestType", "1005");
  73.             string dataSign = encrypt(requestData, AppKey, "UTF-8");
  74.             param.Add("DataSign", HttpUtility.UrlEncode(dataSign, Encoding.UTF8));
  75.             param.Add("DataType", "1");
  76.  
  77.             string result = sendPost(ReqURL, param);
  78.  
  79.             //根据公司业务处理返回的信息......
  80.  
  81.             return result;
  82.         }
  83.  
  84.         /// <summary>
  85.         /// Post方式提交数据,返回网页的源代码
  86.         /// </summary>
  87.         /// <param name="url">发送请求的 URL</param>
  88.         /// <param name="param">请求的参数集合</param>
  89.         /// <returns>远程资源的响应结果</returns>
  90.         private string sendPost(string url, Dictionary<string, string> param)
  91.         {
  92.             string result = "";
  93.             StringBuilder postData = new StringBuilder();
  94.             if (param != null && param.Count > 0)
  95.             {
  96.                 foreach (var p in param)
  97.                 {
  98.                     if (postData.Length > 0)
  99.                     {
  100.                         postData.Append("&");
  101.                     }
  102.                     postData.Append(p.Key);
  103.                     postData.Append("=");
  104.                     postData.Append(p.Value);
  105.                 }
  106.             }
  107.             byte[] byteData = Encoding.GetEncoding("UTF-8").GetBytes(postData.ToString());
  108.             try
  109.             {
  110.  
  111.                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  112.                 request.ContentType = "application/x-www-form-urlencoded";
  113.                 request.Referer = url;
  114.                 request.Accept = "*/*";
  115.                 request.Timeout = 30 * 1000;
  116.                 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)";
  117.                 request.Method = "POST";
  118.                 request.ContentLength = byteData.Length;
  119.                 Stream stream = request.GetRequestStream();
  120.                 stream.Write(byteData, 0, byteData.Length);
  121.                 stream.Flush();
  122.                 stream.Close();
  123.                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  124.                 Stream backStream = response.GetResponseStream();
  125.                 StreamReader sr = new StreamReader(backStream, Encoding.GetEncoding("UTF-8"));
  126.                 result = sr.ReadToEnd();
  127.                 sr.Close();
  128.                 backStream.Close();
  129.                 response.Close();
  130.                 request.Abort();
  131.             }
  132.             catch (Exception ex)
  133.             {
  134.                 result = ex.Message;
  135.             }
  136.             return result;
  137.         }
  138.  
  139.         ///<summary>
  140.         ///电商Sign签名
  141.         ///</summary>
  142.         ///<param name="content">内容</param>
  143.         ///<param name="keyValue">Appkey</param>
  144.         ///<param name="charset">URL编码 </param>
  145.         ///<returns>DataSign签名</returns>
  146.         private string encrypt(String content, String keyValue, String charset)
  147.         {
  148.             if (keyValue != null)
  149.             {
  150.                 return base64(MD5(content + keyValue, charset), charset);
  151.             }
  152.             return base64(MD5(content, charset), charset);
  153.         }
  154.  
  155.         ///<summary>
  156.         /// 字符串MD5加密
  157.         ///</summary>
  158.         ///<param name="str">要加密的字符串</param>
  159.         ///<param name="charset">编码方式</param>
  160.         ///<returns>密文</returns>
  161.         private string MD5(string str, string charset)
  162.         {
  163.             byte[] buffer = System.Text.Encoding.GetEncoding(charset).GetBytes(str);
  164.             try
  165.             {
  166.                 System.Security.Cryptography.MD5CryptoServiceProvider check;
  167.                 check = new System.Security.Cryptography.MD5CryptoServiceProvider();
  168.                 byte[] somme = check.ComputeHash(buffer);
  169.                 string ret = "";
  170.                 foreach (byte a in somme)
  171.                 {
  172.                     if (a < 16)
  173.                         ret += "0" + a.ToString("X");
  174.                     else
  175.                         ret += a.ToString("X");
  176.                 }
  177.                 return ret.ToLower();
  178.             }
  179.             catch
  180.             {
  181.                 throw;
  182.             }
  183.         }
  184.  
  185.         /// <summary>
  186.         /// base64编码
  187.         /// </summary>
  188.         /// <param name="str">内容</param>
  189.         /// <param name="charset">编码方式</param>
  190.         /// <returns></returns>
  191.         private string base64(String str, String charset)
  192.         {
  193.             return Convert.ToBase64String(System.Text.Encoding.GetEncoding(charset).GetBytes(str));
  194.         }
  195.     }
  196. }
  197.  

回复 "如何对接免费不限量的快递查询API接口"

这儿你可以回复上面这条便签

captcha