NET-based example of call docking of free common express bird singular query api interface

Keywords: encoding Mobile JSON Windows

1. Application scenarios
(1) When integrated logistics information inquiry function is applied on PC, mobile or website, the inquiry can be completed only by inputting the single number, without the need for the user to enter the express company.
(2) E-commerce websites should first judge the logistics company through the identification of the single number before subscribing to the express bird when inquiring or subscribing to the shipping order.
2. Need authorization
Free Application Service
For technical documents, please refer to Express Bird's official website api: Free Query Express Interface 100% Safety Guarantee Logistics Instant Query API - Express Bird
(1) Access process:

3. Interface Description/Description
API ID: Click Apply
API Key: Click Apply
Example
(1) The interface only identifies the waybill number and identifies one or more express companies that may belong to it.
(2) The interface does not return the logistics trajectory. Users can complete the action of trajectory query and subscription by combining the instant query interface with the subscription query interface.
(3) Interface recognition will return to one or more express companies, and the returned data will be sorted according to the results of the big express bird data analysis, ranking the top hit rate is higher.
(4) If the recognition fails, the matching result returned by the courier bird is empty.
(5) The message receiving mode supported by the interface is HTTP POST, and the encoding format of the request method (utf-8): "application/x-www-form-urlencoded;charset=utf-8".
(6) Request system-level parameter descriptions:

net example:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace KdGoldAPI
{
    public class KdApiOrderDistinguish
    {
        //Electricity supplier ID
        private string EBusinessID = "Please apply to express bird website http://www.kdniao.com/ServiceApply.aspx";
        //E-commerce encryption private key, courier bird provides, take care of, do not leak
        private string AppKey = "Please apply to express bird website http://www.kdniao.com/ServiceApply.aspx";
        //Request url
        //testing environment
        private string ReqURL = "http://testapi.kdniao.cc:8081/Ebusiness/EbusinessOrderHandle.aspx";
        //Formal environment
        //private string ReqURL = "http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";

        /// <summary>
        /// Json Single Number Recognition
        /// </summary>
        /// <returns></returns>
        public string orderTracesSubByJson()
        {
            string requestData = "{'LogisticCode': '3967950525457'}";

            Dictionary<string, string> param = new Dictionary<string, string>();
            param.Add("RequestData", HttpUtility.UrlEncode(requestData, Encoding.UTF8));
            param.Add("EBusinessID", EBusinessID);
            param.Add("RequestType", "2002");
            string dataSign = encrypt(requestData, AppKey, "UTF-8");
            param.Add("DataSign", HttpUtility.UrlEncode(dataSign, Encoding.UTF8));
            param.Add("DataType", "2");

            string result = sendPost(ReqURL, param);

            //Processing the returned information according to the company's business...

            return result;
        }

        /// <summary>
        /// Post submits data and returns the source code of the web page
        /// </summary>
        /// <param name="url"> URL to send the request </param>
        /// <param name="param">set of parameters requested </param>
        /// < returns > response results of remote resources </returns >
        private string sendPost(string url, Dictionary<string, string> param)
        {
            string result = "";
            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);
                }
            }
            byte[] byteData = Encoding.GetEncoding("UTF-8").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();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream backStream = response.GetResponseStream();
                StreamReader sr = new StreamReader(backStream, Encoding.GetEncoding("UTF-8"));
                result = sr.ReadToEnd();
                sr.Close();
                backStream.Close();
                response.Close();
                request.Abort();
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }
            return result;
        }

        ///<summary>
        /// Sign Signature for E-commerce
        ///</summary>
        /// <param name="content">content</param>
        ///<param name="keyValue">Appkey</param>
        /// <param name="charset">URL encoding </param>
        /// <returns>DataSign signature </returns>
        private string encrypt(String content, String keyValue, String charset)
        {
            if (keyValue != null)
            {
                return base64(MD5(content + keyValue, charset), charset);
            }
            return base64(MD5(content, charset), charset);
        }

        ///<summary>
        /// String MD5 Encryption
        ///</summary>
        /// <param name="str">string to be encrypted </param>
        /// <param name="charset">encoding mode </param>
        /// < returns > ciphertext </returns >
        private string MD5(string str, string charset)
        {
            byte[] buffer = System.Text.Encoding.GetEncoding(charset).GetBytes(str);
            try
            {
                System.Security.Cryptography.MD5CryptoServiceProvider check;
                check = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] somme = check.ComputeHash(buffer);
                string ret = "";
                foreach (byte a in somme)
                {
                    if (a < 16)
                        ret += "0" + a.ToString("X");
                    else
                        ret += a.ToString("X");
                }
                return ret.ToLower();
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// base64 encoding
        /// </summary>
        /// <param name="str">content </param>
        /// <param name="charset">encoding mode </param>
        /// <returns></returns>
        private string base64(String str, String charset)
        {
            return Convert.ToBase64String(System.Text.Encoding.GetEncoding(charset).GetBytes(str));
        }
    }
}

Posted by renno on Tue, 17 Sep 2019 03:27:46 -0700