Example of API call code for the express bird logistics query interface

Source: Internet
Author: User

Example of API call code for the express bird logistics query interface

The express bird logistics query interface is free of charge, with no restrictions, and stability is also good, so the interface is connected to express bird as an example, the user ID and KEY need to apply for http://www.kdniao.com/reg.

Using System; using System. collections. generic; using System. IO; using System. linq; using System. net; using System. text; using System. web;/***** express bird logistics track instant query interface ** @ technology QQ group: 456320272 * @ see: http://www.kdniao.com/YundanChaxunAPI.aspx * @ Copyright: the E-commerce ID and private key in the DEMO of Shenzhen quickmoney Data Technology Service Co., Ltd. are only available for testing. Please register an account separately in the official environment * more than 500 queries per day, we recommend that you access our logistics Track subscription push interface */namespace Test {public class KdApiSearchDemoYS {// e-commerce ID private string EBusinessID = "ID needs to apply to the official website: http://www.kdniao.com/ServiceApply.aspx "; // Private Key encrypted for e-commerce, provided by the courier bird. Be sure to keep it confidential. Do not disclose private string AppKey =" Key. You need to apply to the official website: http://www.kdniao.com/ServiceApply.aspx "; // Request url private string ReqURL =" http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx "; /// <Summary> // query the order logistics track in Json format /// </summary> /// <returns> </returns> public string getOrderTracesByJson (string shipperCode, string expNo) {string requestData = "{'ordercode':'', 'shippercode': '"+ ShipperCode +"', 'logisticcode ': '"+ expNo +"'} "; Dictionary <string, string> param = new Dictionary <string, string> (); param. add ("RequestData", HttpUtility. urlEncode (requestData, Encoding. UTF8); param. ad D ("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 (ReqURL, param); // process the returned information according to the company's business ...... return result;} // <summary> // query the order logistics track in XML format // </summary> // <returns> </returns> public string getOrderTrace SByXml () {string requestData = "<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> "+" <Content> "+" <OrderCode> </OrderCode> "+" <ShipperCode> SF </ShipperCode> "+" <LogisticCode> 589707398027 </LogisticCode> "+ "</Content> "; 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, App Key, "UTF-8"); param. add ("DataSign", HttpUtility. urlEncode (dataSign, Encoding. UTF8); param. add ("DataType", "1"); string result = sendPost (ReqURL, param); // process the returned information according to the company's business ...... return result;} // <summary> // submit data in Post mode, return the source code of the webpage /// </summary> /// <param name = "url"> send request URL </param> /// <param name = "param"> request parameter set </param> /// <returns> Remote resource response result </returns> private string sendPost (string url, dict Ionary <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. R Eferer = 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.20.6.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 // /</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 method </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" +. toString ("X"); else ret + =. toString ("X");} return ret. toLower () ;}catch {throw ;}} /// <summary> /// base64 encoded // </summary> /// <param name = "str"> content </param> /// <param name = "charset"> encoding method </param> // <returns> </returns> private string base64 (String str, string charset) {return Convert. toBase64String (System. text. encoding. getEncoding (charset ). getBytes (str ));}}}

  

  Aspx interface Example
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "KdApiSearchYS. aspx. cs" Inherits = "Test. KdApiSearchYS" %> <! DOCTYPE html> 

Aspx. cs code

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace Test{    public partial class KdApiSearchYS : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {        }        protected void Button_Click(object sender, EventArgs e)        {            KdApiSearchDemoYS search = new KdApiSearchDemoYS();            string result = search.getOrderTracesByJson(this.txtShipperCode.Text.Trim(), this.txtLogisticCode.Text.Trim());            Response.Write(result);        }    }}

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.