您好,登錄后才能下訂單哦!
本篇內容介紹了“C#怎么實現快遞鳥API接口簽名”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
//電商ID string eEBusinessID = "test1617571"; //電商加密私鑰,快遞鳥提供,注意保管,不要泄漏 string appKey= "554343b2-7252-439b-b4eb-1af42c8f2175"; //請求url string reqURL = "http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json"; //請求指令 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的方法就能獲取到里面的字段信息。
///<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; }
“C#怎么實現快遞鳥API接口簽名”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。