C#實(shí)現(xiàn)快遞api接口調(diào)用方法
更新時(shí)間:2015年03月23日 14:44:24 投稿:hebedich
這篇文章主要介紹了C#實(shí)現(xiàn)快遞api接口調(diào)用方法,主要是通過(guò)快遞API網(wǎng)接口的服務(wù),使用的時(shí)候直接申請(qǐng)個(gè)接口UID即可,有需要的小伙伴來(lái)參考下吧。
無(wú)平臺(tái)限制,依賴(lài)于快遞api網(wǎng)接口
----------------實(shí)體類(lèi)
[DataContract]
public class SyncResponseEntity
{
public SyncResponseEntity() { }
/// <summary>
/// 需要查詢(xún)的快遞代號(hào)
/// </summary>
[DataMember(Order = 0, Name = "id")]
public string ID { get; set; }
/// <summary>
/// 需要查詢(xún)的快遞名稱(chēng)
/// </summary>
[DataMember(Order = 1, Name = "name")]
public string Name { get; set; }
/// <summary>
/// 需要查詢(xún)的快遞單號(hào)
/// </summary>
[DataMember(Order = 2, Name = "order")]
public string Order { get; set; }
/// <summary>
/// 消息內(nèi)容
/// </summary>
[DataMember(Order = 5, Name = "message")]
public string Message { get; set; }
/// <summary>
/// 服務(wù)器狀態(tài)
/// </summary>
[DataMember(Order = 6, Name = "errcode")]
public string ErrCode { get; set; }
/// <summary>
/// 運(yùn)單狀態(tài)
/// </summary>
[DataMember(Order = 7, Name = "status")]
public int Status { get; set; }
/// <summary>
/// 跟蹤記錄
/// </summary>
[DataMember(Order = 8, Name = "data")]
public List<Order> Data { get; set; }
}
[DataContract(Name = "data")]
public class Order
{
public Order() { }
public Order(string time, string content)
{
this.Time = time;
this.Content = content;
}
[DataMember(Order = 0, Name = "time")]
public string Time { get; set; }
[DataMember(Order = 1, Name = "content")]
public string Content { get; set; }
}
---------調(diào)用方法
public static int uid = Utils.GetAppConfig<int>("KUAIDIAPI_UID", 0);
public static string sync_url = Utils.GetAppConfig<string>("KUAIDIAPI_SYNC_URL", string.Empty);
public static string key = Utils.GetAppConfig<string>("KUAIDIAPI_KEY", string.Empty);
/// <summary>
/// 同步單號(hào)查詢(xún)方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="id"></param>
/// <param name="order"></param>
/// <param name="isSign"></param>
/// <param name="isLast"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static T APIQueryDataSYNC<T>(string id,
string order,
bool isSign,
bool isLast,
T defaultValue)
{
try
{
string currTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string currKey = key;
if (isSign)
{
currKey = Utils.GetSign(uid, key, id, order, currTime);
currKey += "&issign=true";
}
string url = sync_url + string.Format("?uid={0}&key={1}&id={2}&order={3}&time={4}",
uid, currKey, id, order, HttpUtility.UrlEncode(currTime));
string html = Utils.GET_WebRequestHTML("utf-8", url);
if (!string.IsNullOrEmpty(html))
return Utils.JsonToObj<T>(html, defaultValue);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return defaultValue;
}
}
/// <summary>
/// 輔助工具類(lèi)
/// </summary>
public class Utils
{
public static string GetSign(int uid, string key, string id, string order, string time)
{
string sign = string.Format("uid={0}&key={1}&id={2}&order={3}&time={4}",
uid,
key,
id,
HttpUtility.UrlEncode(order.ToLower()),
HttpUtility.UrlEncode(time));
return Md5Encrypt(sign.ToLower(), "utf-8");
}
public static string Md5Encrypt(string strToBeEncrypt, string encodingName)
{
MD5 md5 = new MD5CryptoServiceProvider();
Byte[] FromData = System.Text.Encoding.GetEncoding(encodingName).GetBytes(strToBeEncrypt);
Byte[] TargetData = md5.ComputeHash(FromData);
string Byte2String = "";
for (int i = 0; i < TargetData.Length; i++)
{
Byte2String += TargetData[i].ToString("x2");
}
return Byte2String;
}
public static T GetRequest<T>(string key, T defaultValue)
{
string value = HttpContext.Current.Request[key];
if (string.IsNullOrEmpty(value))
{
return defaultValue;
}
else
{
try
{
return (T)Convert.ChangeType(value, typeof(T));
}
catch
{
return defaultValue;
}
}
}
public static T GetAppConfig<T>(string key, T defaultValue)
{
string value = ConfigurationManager.AppSettings[key];
if (string.IsNullOrEmpty(value))
{
return defaultValue;
}
else
{
try
{
return (T)Convert.ChangeType(value, typeof(T));
}
catch
{
return defaultValue;
}
}
}
public static string ObjToJson<T>(T data)
{
try
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(data.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, data);
return Encoding.UTF8.GetString(ms.ToArray());
}
}
catch
{
return null;
}
}
public static T JsonToObj<T>(string json, T defaultValue)
{
try
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
object obj = serializer.ReadObject(ms);
return (T)Convert.ChangeType(obj, typeof(T));
}
}
catch
{
return defaultValue;
}
}
public static T XmlToObj<T>(string xml, T defaultValue)
{
try
{
System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
{
object obj = serializer.ReadObject(ms);
return (T)Convert.ChangeType(obj, typeof(T));
}
}
catch
{
return defaultValue;
}
}
public static string ObjToXml<T>(T data)
{
System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, data);
return Encoding.UTF8.GetString(ms.ToArray());
}
}
public static string GET_WebRequestHTML(string encodingName, string htmlUrl)
{
string html = string.Empty;
try
{
Encoding encoding = Encoding.GetEncoding(encodingName);
WebRequest webRequest = WebRequest.Create(htmlUrl);
HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, encoding);
html = streamReader.ReadToEnd();
httpWebResponse.Close();
responseStream.Close();
streamReader.Close();
}
catch (WebException ex)
{
throw new Exception(ex.Message);
}
return html;
}
/// <summary>
/// 將網(wǎng)址類(lèi)容轉(zhuǎn)換成文本字符串 post請(qǐng)求
/// </summary>
/// <param name="data">要post的數(shù)據(jù)</param>
/// <param name="url">目標(biāo)url</param>
/// <returns>服務(wù)器響應(yīng)</returns>
public static string POST_HttpWebRequestHTML( string encodingName,
string htmlUrl,
string postData)
{
string html = string.Empty;
try
{
Encoding encoding = Encoding.GetEncoding(encodingName);
byte[] bytesToPost = encoding.GetBytes(postData);
WebRequest webRequest = WebRequest.Create(htmlUrl);
HttpWebRequest httpRequest = webRequest as System.Net.HttpWebRequest;
httpRequest.Method = "POST";
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = bytesToPost.Length;
httpRequest.Timeout = 15000;
httpRequest.ReadWriteTimeout = 15000;
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytesToPost, 0, bytesToPost.Length);
requestStream.Close();
HttpWebResponse httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, encoding);
html = streamReader.ReadToEnd();
}
catch (WebException ex)
{
throw new Exception(ex.Message);
}
return html;
}
}
/// <summary>
/// 接口類(lèi)型
/// </summary>
public enum APIType
{
//同步查詢(xún)
SYNC = 1
}
基本上代碼都在上面。在帶www.kuaidiapi.cn上申請(qǐng)一個(gè)uid就大功告成。
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
您可能感興趣的文章:
- C# web api返回類(lèi)型設(shè)置為json的兩種方法
- C#中通過(guò)API實(shí)現(xiàn)的打印類(lèi) 實(shí)例代碼
- C#通過(guò)WIN32 API實(shí)現(xiàn)嵌入程序窗體
- 使用C#調(diào)用系統(tǒng)API實(shí)現(xiàn)內(nèi)存注入的代碼
- ASP.NET(C#) Web Api通過(guò)文件流下載文件的實(shí)例
- c#調(diào)用api控制windows關(guān)機(jī)示例(可以重啟/注銷(xiāo))
- C#利用win32 Api 修改本地系統(tǒng)時(shí)間、獲取硬盤(pán)序列號(hào)
- C#獲取USB事件API實(shí)例分析
- c#之利用API函數(shù)實(shí)現(xiàn)動(dòng)畫(huà)窗體的方法詳解
- C# API中模型與它們的接口設(shè)計(jì)詳解
相關(guān)文章
C#中使用1.7版本驅(qū)動(dòng)操作MongoDB簡(jiǎn)單例子
這篇文章主要介紹了C#中使用1.7版本驅(qū)動(dòng)操作MongoDB簡(jiǎn)單例子,本文給出了連接MongoDB、操作MongoDB數(shù)據(jù)等例子,需要的朋友可以參考下2015-01-01
基于C#實(shí)現(xiàn)的多邊形沖突檢測(cè)實(shí)例
這篇文章主要給大家介紹了基于C#實(shí)現(xiàn)的多邊形沖突檢測(cè)的相關(guān)資料,文中介紹的方法并未使用第三方類(lèi)庫(kù),可以完美解決這個(gè)問(wèn)題,需要的朋友可以參考下2021-07-07
C# ODP.NET 調(diào)用Oracle函數(shù)返回值時(shí)報(bào)錯(cuò)的一個(gè)解決方案
這篇文章主要介紹了C# ODP.NET 調(diào)用Oracle函數(shù)返回值時(shí)報(bào)錯(cuò)的一個(gè)解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

