无平台限制,依赖于快递api网接口
----------------实体类 [DataContract] publicclassSyncResponseEntity { publicSyncResponseEntity(){} ///<summary> ///需要查询的快递代号 ///</summary> [DataMember(Order=0,Name="id")] publicstringID{get;set;} ///<summary> ///需要查询的快递名称 ///</summary> [DataMember(Order=1,Name="name")] publicstringName{get;set;} ///<summary> ///需要查询的快递单号 ///</summary> [DataMember(Order=2,Name="order")] publicstringOrder{get;set;} ///<summary> ///消息内容 ///</summary> [DataMember(Order=5,Name="message")] publicstringMessage{get;set;} ///<summary> ///服务器状态 ///</summary> [DataMember(Order=6,Name="errcode")] publicstringErrCode{get;set;} ///<summary> ///运单状态 ///</summary> [DataMember(Order=7,Name="status")] publicintStatus{get;set;} ///<summary> ///跟踪记录 ///</summary> [DataMember(Order=8,Name="data")] publicList<Order>Data{get;set;} } [DataContract(Name="data")] publicclassOrder { publicOrder(){} publicOrder(stringtime,stringcontent) { this.Time=time; this.Content=content; } [DataMember(Order=0,Name="time")] publicstringTime{get;set;} [DataMember(Order=1,Name="content")] publicstringContent{get;set;} } ---------调用方法 publicstaticintuid=Utils.GetAppConfig<int>("KUAIDIAPI_UID",0); publicstaticstringsync_url=Utils.GetAppConfig<string>("KUAIDIAPI_SYNC_URL",string.Empty); publicstaticstringkey=Utils.GetAppConfig<string>("KUAIDIAPI_KEY",string.Empty); ///<summary> ///同步单号查询方法 ///</summary> ///<typeparamname="T"></typeparam> ///<paramname="id"></param> ///<paramname="order"></param> ///<paramname="isSign"></param> ///<paramname="isLast"></param> ///<paramname="defaultValue"></param> ///<returns></returns> publicstaticTAPIQueryDataSYNC<T>(stringid, stringorder, boolisSign, boolisLast, TdefaultValue) { try { stringcurrTime=DateTime.Now.ToString("yyyy-MM-ddHH:mm:ss"); stringcurrKey=key; if(isSign) { currKey=Utils.GetSign(uid,key,id,order,currTime); currKey+="&issign=true"; } stringurl=sync_url+string.Format("?uid={0}&key={1}&id={2}&order={3}&time={4}", uid,currKey,id,order,HttpUtility.UrlEncode(currTime)); stringhtml=Utils.GET_WebRequestHTML("utf-8",url); if(!string.IsNullOrEmpty(html)) returnUtils.JsonToObj<T>(html,defaultValue); } catch(Exceptionex) { thrownewException(ex.Message); } returndefaultValue; } } ///<summary> ///辅助工具类 ///</summary> publicclassUtils { publicstaticstringGetSign(intuid,stringkey,stringid,stringorder,stringtime) { stringsign=string.Format("uid={0}&key={1}&id={2}&order={3}&time={4}", uid, key, id, HttpUtility.UrlEncode(order.ToLower()), HttpUtility.UrlEncode(time)); returnMd5Encrypt(sign.ToLower(),"utf-8"); } publicstaticstringMd5Encrypt(stringstrToBeEncrypt,stringencodingName) { MD5md5=newMD5CryptoServiceProvider(); Byte[]FromData=System.Text.Encoding.GetEncoding(encodingName).GetBytes(strToBeEncrypt); Byte[]TargetData=md5.ComputeHash(FromData); stringByte2String=""; for(inti=0;i<TargetData.Length;i++) { Byte2String+=TargetData[i].ToString("x2"); } returnByte2String; } publicstaticTGetRequest<T>(stringkey,TdefaultValue) { stringvalue=HttpContext.Current.Request[key]; if(string.IsNullOrEmpty(value)) { returndefaultValue; } else { try { return(T)Convert.ChangeType(value,typeof(T)); } catch { returndefaultValue; } } } publicstaticTGetAppConfig<T>(stringkey,TdefaultValue) { stringvalue=ConfigurationManager.AppSettings[key]; if(string.IsNullOrEmpty(value)) { returndefaultValue; } else { try { return(T)Convert.ChangeType(value,typeof(T)); } catch { returndefaultValue; } } } publicstaticstringObjToJson<T>(Tdata) { try { DataContractJsonSerializerserializer=newDataContractJsonSerializer(data.GetType()); using(MemoryStreamms=newMemoryStream()) { serializer.WriteObject(ms,data); returnEncoding.UTF8.GetString(ms.ToArray()); } } catch { returnnull; } } publicstaticTJsonToObj<T>(stringjson,TdefaultValue) { try { System.Runtime.Serialization.Json.DataContractJsonSerializerserializer=newSystem.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T)); using(MemoryStreamms=newMemoryStream(Encoding.UTF8.GetBytes(json))) { objectobj=serializer.ReadObject(ms); return(T)Convert.ChangeType(obj,typeof(T)); } } catch { returndefaultValue; } } publicstaticTXmlToObj<T>(stringxml,TdefaultValue) { try { System.Runtime.Serialization.DataContractSerializerserializer=newSystem.Runtime.Serialization.DataContractSerializer(typeof(T)); using(MemoryStreamms=newMemoryStream(Encoding.UTF8.GetBytes(xml))) { objectobj=serializer.ReadObject(ms); return(T)Convert.ChangeType(obj,typeof(T)); } } catch { returndefaultValue; } } publicstaticstringObjToXml<T>(Tdata) { System.Runtime.Serialization.DataContractSerializerserializer=newSystem.Runtime.Serialization.DataContractSerializer(typeof(T)); using(MemoryStreamms=newMemoryStream()) { serializer.WriteObject(ms,data); returnEncoding.UTF8.GetString(ms.ToArray()); } } publicstaticstringGET_WebRequestHTML(stringencodingName,stringhtmlUrl) { stringhtml=string.Empty; try { Encodingencoding=Encoding.GetEncoding(encodingName); WebRequestwebRequest=WebRequest.Create(htmlUrl); HttpWebResponsehttpWebResponse=(HttpWebResponse)webRequest.GetResponse(); StreamresponseStream=httpWebResponse.GetResponseStream(); StreamReaderstreamReader=newStreamReader(responseStream,encoding); html=streamReader.ReadToEnd(); httpWebResponse.Close(); responseStream.Close(); streamReader.Close(); } catch(WebExceptionex) { thrownewException(ex.Message); } returnhtml; } ///<summary> ///将网址类容转换成文本字符串post请求 ///</summary> ///<paramname="data">要post的数据</param> ///<paramname="url">目标url</param> ///<returns>服务器响应</returns> publicstaticstringPOST_HttpWebRequestHTML(stringencodingName, stringhtmlUrl, stringpostData) { stringhtml=string.Empty; try { Encodingencoding=Encoding.GetEncoding(encodingName); byte[]bytesToPost=encoding.GetBytes(postData); WebRequestwebRequest=WebRequest.Create(htmlUrl); HttpWebRequesthttpRequest=webRequestasSystem.Net.HttpWebRequest; httpRequest.Method="POST"; httpRequest.UserAgent="Mozilla/4.0(compatible;MSIE7.0;WindowsNT5.2;.NETCLR1.1.4322;.NETCLR2.0.50727)"; httpRequest.ContentType="application/x-www-form-urlencoded"; httpRequest.ContentLength=bytesToPost.Length; httpRequest.Timeout=15000; httpRequest.ReadWriteTimeout=15000; StreamrequestStream=httpRequest.GetRequestStream(); requestStream.Write(bytesToPost,0,bytesToPost.Length); requestStream.Close(); HttpWebResponsehttpWebResponse=(HttpWebResponse)httpRequest.GetResponse(); StreamresponseStream=httpWebResponse.GetResponseStream(); StreamReaderstreamReader=newStreamReader(responseStream,encoding); html=streamReader.ReadToEnd(); } catch(WebExceptionex) { thrownewException(ex.Message); } returnhtml; } } ///<summary> ///接口类型 ///</summary> publicenumAPIType { //同步查询 SYNC=1 }
基本上代码都在上面。在带www.kuaidiapi.cn上申请一个uid就大功告成。
上述就是C#学习教程:C#实现快递api接口调用方法分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/906065.html