Home > Net >  C # express query demo (API call Courier bird single number query interface)
C # express query demo (API call Courier bird single number query interface)

Time:09-21

method invocation code
//electricity ID 
String eEBusinessID="test1617571";
//electricity encryption private key, provide express bird, didn't go to express bird registered http://www.kdniao.com/reg attention preservation, don't leak
String appKey="b2-7252-439 - b - 554343 af42c8f2175 b4eb - 1".
//request url
String reqURL="http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json";
//request instructions
String reqType="1002";
//2 - json
String dataType="2";
//character encoding USES the utf-8
String charset="utf-8";
//JSON string string
String jsonStr=
"{" OrderCode \ ": " \ ", \ "ShipperCode " : \ "SF, ", \ "LogisticCode " : \ "118461988807 "} ";
//for the MD5 encryption (jsonStr + APIKey)
String md5Str=MD5 (jsonStr + apiKey, charset);
//the md5Str Base64 encoding
String base64Str=base64 (md5Str charset);
//URL encoding (utf-8)
String datasign=HttpUtility. UrlEncode (base64Str charset);
//request message parameter
String postStr="RequestType=reqType& EBusinessID=eEBusinessID& RequestData=https://bbs.csdn.net/topics/jsonStr
& DataSign=datasign& DataType=DataType ";
//communication protocol using Http Post request way return trajectory data
The string post=SendPost (reqURL postStr);
//to get to the post data is express birds return the complete message, and then write a parsing json method can access to the inside of the field information,


c # encapsulation method
///& lt; summary> 
///string MD5 encryption
///& lt;/summary>
///& lt; Param name="STR" & gt; To be encrypted string & lt;/param>
///& lt; Param name="charset" & gt; Encoding & lt;/param>
///& lt; Returns> Cipher & lt;/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.Com puteHash (buffer);
String ret="";
Foreach (byte a in somme)
{
If (a & lt; 16)
Ret +="0" + a.T oString (" X ");
The else
Ret +=a.T oString (" X ");
}
Return ret. ToLower ();
}
Catch
{
Throw;
}
}
///& lt; summary>
///base64 encoding
///& lt;/summary>
///& lt; Param name="STR" & gt; The content & lt;/param>
///& lt; Param name="charset" & gt; Encoding & lt;/param>
///& lt; Returns>
Private string base64 (string STR, string charset)
{
Return the Convert. ToBase64String (System. Text. Encoding. GetEncoding (charset). GetBytes (STR));
}
///& lt; summary>
///Post way to submit data, returns the page source code
///& lt;/summary>
///& lt; Param name="url" & gt; Send the request URL
///& lt; Param name="postData" & gt; Request message parameter & lt;/param>
///& lt; Returns> The response of the remote resource results & lt;/returns>
Private string SendPost (url string, the string postData)
{
string result="";
Byte [] byteData=https://bbs.csdn.net/topics/Encoding.GetEncoding (" utf-8 "). GetBytes (postData. The ToString ());
Try
{
HttpWebRequest request=(HttpWebRequest) WebRequest. Create (url);
Request. The ContentType="application/x - WWW - form - urlencoded";
Request.=Referer url;
Request. Accept="*/*";
Request. The Timeout=30 * 1000;
Request. UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; The.net CLR 2.0.50727; The.net CLR 3.0.04506.648; The.net CLR 3.0.4506.2152; The.net CLR 3.5.30729) ";
Request. The Method="POST";
Request. ContentLength=byteData. Length;
Stream, Stream=request. GetRequestStream ();
Stream. Write (byteData, 0, byteData. Length);
Stream. Flush ();
stream.Close();
HttpWebResponse response=(HttpWebResponse) request. The method GetResponse ();
Stream backStream=response. GetResponseStream ();
StreamReader sr=new StreamReader (backStream, Encoding GetEncoding (" utf-8 "));
Result=sr. ReadToEnd ();
sr.Close();
BackStream. Close ();
The response. The Close ();
Request. The Abort ();
}
The catch (Exception ex)
{
Result=ex. ToString ();
}
return result;
}


CodePudding user response:

Is this a share
  •  Tags:  
  • C#
  • Related