Express query C #,

Source: Internet
Author: User

Express query C #,

// E-commerce ID
Private string EBusinessID = "1257164 ";
// The private key is encrypted in e-commerce and provided by the courier bird. Keep it safe and do not leak it.
Private string AppKey = "63a33b7c-464c-4de6-b4a3-6e1fc19da51c ";
// 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 postkuaidi (string com, string nu)
{
Dictionary <string, string> list = new Dictionary <string, string> ();
# Region express
List. Add ("postal mail", "YZPY ");
List. Add ("China Post", "YZPY ");
List. Add ("China Post", "YZPY ");
List. Add ("Shentong express", "STO ");

# Endregion
String typeCom = "";
Foreach (var dc in list)
{
If (com = dc. Key)
{
TypeCom = dc. Value;
}
}
String requestData = "{OrderCode:'', ShipperCode: '"+ typeCom +"', 'logisticcode': '"+ nu + "'}";

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 (ReqURL, param );

// Process the returned information according to the company's business ......
JObject jo = JObject. Parse (result );
Return jo. ToString ();
}

 

/// <Summary>
/// Submit data in Post mode and return the source code of the webpage
/// </Summary>
/// <Param name = "url"> URL of the Request </param>
/// <Param name = "param"> set of Request Parameters </param>
/// <Returns> Remote resource response result </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.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" + 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 method </param>
/// <Returns> </returns>
Private string base64 (String str, String charset)
{
Return Convert. ToBase64String (System. Text. Encoding. GetEncoding (charset). GetBytes (str ));
}

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.