龙空技术网

C# API接口签名 快递接口查询

老杨占线 55

前言:

现在朋友们对“singpost到国内怎么查询”大概比较着重,兄弟们都想要知道一些“singpost到国内怎么查询”的相关资讯。那么小编也在网摘上收集了一些对于“singpost到国内怎么查询””的相关资讯,希望小伙伴们能喜欢,同学们快快来了解一下吧!

方法调用的代码实现

//电商ID 去快递鸟注册string eEBusinessID = "test1617571"; //电商加密私钥,快递鸟提供,注意保管,不要泄漏string appKey= "554343b2-7252-439b-b4eb-1af42c8f2175"; //请求urlstring reqURL = ";;//请求指令 string reqType="1002";//2-json string dataType = "2"; //字符编码采用UTF-8 string charset = "UTF-8"; //JSON字符串string string jsonStr = "{\"OrderCode\":\"\",\"ShipperCode\":\"SF\",\"LogisticCode\":\"118461988807\"}" ;//把(jsonStr+APIKey)进行MD5加密string md5Str=MD5(jsonStr + apiKey, charset);//把md5Str 进行Base64编码string base64Str=base64(md5Str,charset);//进行URL编码 (utf-8)string datasign = HttpUtility.UrlEncode(base64Str, charset); //请求报文参数 string postStr = "RequestType=reqType&EBusinessID= eEBusinessID&RequestData=jsonStr &DataSign= datasign&DataType=dataType"; //通讯协议使用Http协议Post请求方式 返回轨迹数据string post = SendPost(reqURL, postStr);//获取到的post数据就是快递鸟返回的完整报文,接下来自己写一个解析json的方法就能获取到里面的字段信息。
C#封装的方法
///<summary> /// 字符串MD5加密 ///</summary> ///<param name="str">要加密的字符串</param> ///<param name="charset">编码方式</param> ///<returns>密文</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编码 /// </summary> /// <param name="str">内容</param> /// <param name="charset">编码方式</param> /// <returns></returns> private string base64(String str, String charset) { return Convert.ToBase64String(System.Text.Encoding.GetEncoding(charset).GetBytes(str));} /// <summary> /// Post方式提交数据,返回网页的源代码 /// </summary> /// <param name="url">发送请求的 URL</param> /// <param name="postData">请求报文参数</param> /// <returns>远程资源的响应结果</returns> private string SendPost(string url, string postData) { string result = ""; 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.ToString(); } return result; }

标签: #singpost到国内怎么查询