C#.net Implementation of Zhongtong Express Single Query Express Bird API Interface

In the last article, we introduced a logistics service provider. We recommend you to use Express Bird Interface. It mainly describes how to register an account, get a key, and find a registered address. I'll send it:

http://kdniao.com/reg

Today we're going to talk about how to use the interface provided by courier birds for instant queries.

Before we develop, let's first understand what Instant Query is. In my understanding, we provide shipping bill number and courier company code, and then call the query interface provided by courier bird to query the shipping status of shipping bill number. Through this interface, we can know the time when the package was received, sent, received, signed and received, and if the experience is better.One point, large data allows you to analyze the expected delivery time of this package.

Here's what I get by calling the instant query interface provided by the Express Bird interface:

The visual effect is negligible, which is a screenshot of my project application. Once we get track information, we can finally present it to our customers in our own page style.

 

Okay, now let's talk about how it works!

First we have the resources we need.

Test Merchant ID:

test1617571

 

Test API key:

554343b2-7252-439b-b4eb-1af42c8f2175 (This Key is used only in test environments)

 

API test address:

http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json

 

My project was developed with C#, so I'll take C# development as an example to illustrate, later requirements can also provide Java, PHP articles

 

Let's first read the interface documentation provided by the Express Bird website

 

Request system-level parameter descriptions:

Remarks: R-Required, O-Optional, C-Message This parameter is optional under certain conditions.

System-level parameters are mentioned here, which are equivalent to common parameters that must be passed in order to call each interface.

Interface parameters:

 

Interface parameters, also known as business parameters, request different business interfaces, parameter fields, content are different, is following the changes of business, here we implement the real-time query interface, courier bird official network requires that the courier company code and logistics number must be passed

Logistics number is well understood, that is, the shipping number on the courier bill of lading. The courier company code is the code that must pass courier bird support. You may ask, how can I know which courier companies courier bird supports? Don't worry, tell you immediately.

Download courier company code:

http://www.kdniao.com/documents

As mentioned in the previous article, download it and you will understand it in seconds, haha!

As in the track screenshot before me, it is a Zhongtong track data, the code of Zhongtong Express is ZTO, and the logistics number is 78120038107849

Business parameter message combinations are as follows:

{'OrderCode':'','ShipperCode':'ZTO','LogisticCode':'78120038107849'}

 

Full message requested:

RequestData=%7b%27OrderCode%27%3a%27%27%2c%27ShipperCode%27%3a%27ZTO%27%2c%27LogisticCode%27%3a%2778120038107849%27%7d&EBusinessID=1617571&RequestType=1002&DataSign=YzBmYTViYmExZmFhOGY1ZTY3MWY5OGFjYWRhNWVjNjU%3d&DataType=2

 

Message information returned:

 

{
    "LogisticCode": "78120038107849",
    "ShipperCode": "ZTO",
    "Traces": [
        {
            "AcceptStation": "[Jiyuan City [Jiyuan] (0391)-6965909) Zhang Xia of 18839032214 has been collected",
            "AcceptTime": "2020-01-16 18:30:33"
        },
        {
            "AcceptStation": "[Jiyuan City Express departure has been sent to Shenzhen Center",
            "AcceptTime": "2020-01-16 18:36:41"
        },
        {
            "AcceptStation": "[Xinxiang City Express has arrived",
            "AcceptTime": "2020-01-16 22:45:49"
        },
        {
            "AcceptStation": "[Xinxiang City Express departure has been sent to Shenzhen Center",
            "AcceptTime": "2020-01-16 22:47:48"
        },
        {
            "AcceptStation": "[Shenzhen Express has arrived at Shenzhen Center",
            "AcceptTime": "2020-01-18 04:05:46"
        },
        {
            "AcceptStation": "[Shenzhen) Express departure from Shenzhen Center has been sent to Shenzhen Longhua",
            "AcceptTime": "2020-01-18 08:34:46"
        },
        {
            "AcceptStation": "[Shenzhen Express has arrived in Longhua, Shenzhen",
            "AcceptTime": "2020-01-18 13:14:10"
        },
        {
            "AcceptStation": "[Chen Zhilong of Shenzhen Longhua-Wang Ying (13923773902) is dispatching for the first time, Please keep the phone unblocked,And wait patiently (95720 is the exclusive number for outbound calls of Midway couriers, please answer with confidence)",
            "AcceptTime": "2020-01-18 16:38:35"
        },
        {
            "AcceptStation": "[Shenzhen Express has been sent by Xinmao Garden in Fengchao A area(Toyota Smart Express Cabinet)]Sign on behalf of, Please contact us if you have any questions (13923773902) / 4000633333,18025858922), Thank you for using Zhongtong Express, Looking forward to serving you again!",
            "AcceptTime": "2020-01-18 17:32:15"
        }
    ],
    "State": "3",
    "EBusinessID": "1617571",
    "Success": true
}
C#Key Code:

  string requestData = "{'OrderCode':'','ShipperCode':'ZTO','LogisticCode':'78120038107849'}";

 

  string dataSign = encrypt(requestData, "554343b2-7252-439b-b4eb-1af42c8f2175", "UTF-8");

 

 

/// <summary>

    ///Instant inquiry on Logistics tracks

    /// </summary>

    /// <param name="url">http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json</param>

    /// <param name="eBusinessID">test1617571</param>

    /// <param name="appKey">554343b2-7252-439b-b4eb-1af42c8f2175</param>

    /// <param name="requestData">{'OrderCode':'','ShipperCode':'ZTO','LogisticCode':'78120038107849'}</param>

    /// <returns></returns>

    public string getOrderTracesByJson(string url,string eBusinessID, string appKey, string requestData)

    {

     

 

        Dictionary<string, string> param = new Dictionary<string, string>();

        param.Add("RequestData", HttpUtility.UrlEncode(requestData, Encoding.UTF8));

        param.Add("EBusinessID", eBusinessID);

        param.Add("RequestType", "1002");

        string dataSign = encrypt(requestData, appKey, "UTF-8");

        param.Add("DataSign", HttpUtility.UrlEncode(dataSign, Encoding.UTF8));

        param.Add("DataType", "2");

 

        string result = sendPost(url, param);

 

 

        return result;

    }

 

    /// <summary>

    /// Post submit data, return source code of web page

    /// </summary>

    /// <param name="url">URL to send request</param>

    /// <param name="param">set of requested parameters </param>

    /// <returns>Response results from 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);

            }

        }

       // return postData.ToString();

        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>

    ///E-commerce Sign ature

    ///</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</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</param>

    /// <returns></returns>

    private string base64(String str, String charset)

    {

        return Convert.ToBase64String(System.Text.Encoding.GetEncoding(charset).GetBytes(str));

    }

Keywords: Programming encoding JSON Java PHP

Added by forced4 on Wed, 15 Apr 2020 04:59:12 +0300