C#实现快递api接口调用方法
作者:hebedich 时间:2022-06-15 01:31:58
无平台限制,依赖于快递api网接口
----------------实体类
[DataContract]
public class SyncResponseEntity
{
public SyncResponseEntity() { }
/// <summary>
/// 需要查询的快递代号
/// </summary>
[DataMember(Order = 0, Name = "id")]
public string ID { get; set; }
/// <summary>
/// 需要查询的快递名称
/// </summary>
[DataMember(Order = 1, Name = "name")]
public string Name { get; set; }
/// <summary>
/// 需要查询的快递单号
/// </summary>
[DataMember(Order = 2, Name = "order")]
public string Order { get; set; }
/// <summary>
/// 消息内容
/// </summary>
[DataMember(Order = 5, Name = "message")]
public string Message { get; set; }
/// <summary>
/// 服务器状态
/// </summary>
[DataMember(Order = 6, Name = "errcode")]
public string ErrCode { get; set; }
/// <summary>
/// 运单状态
/// </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; }
}
---------调用方法
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>
/// 同步单号查询方法
/// </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>
/// 辅助工具类
/// </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>
/// 将网址类容转换成文本字符串 post请求
/// </summary>
/// <param name="data">要post的数据</param>
/// <param name="url">目标url</param>
/// <returns>服务器响应</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>
/// 接口类型
/// </summary>
public enum APIType
{
//同步查询
SYNC = 1
}
基本上代码都在上面。在带www.kuaidiapi.cn上申请一个uid就大功告成。
以上所述就是本文的全部内容了,希望大家能够喜欢。
标签:C#,快递,api接口
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#图表算法之无向图
2021-08-08 22:50:20
![](https://img.aspxhome.com/file/2023/4/113854_0s.jpg)
Java swing 图像处理多种效果实现教程
2022-08-16 20:15:08
![](https://img.aspxhome.com/file/2023/0/77960_0s.jpg)
Java Fluent Mybatis 项目工程化与常规操作详解流程篇 上
2022-08-28 16:47:28
![](https://img.aspxhome.com/file/2023/0/100670_0s.png)
如何为Spring Cloud Gateway加上全局过滤器
2022-06-19 09:14:47
Android ViewPager无限循环实现底部小圆点动态滑动
2022-03-21 11:04:28
![](https://img.aspxhome.com/file/2023/9/122109_0s.png)
深入解析Spring Cloud内置的Zuul过滤器
2022-05-16 12:49:04
SpringBoot集成RabbitMQ和概念介绍
2022-06-25 18:36:24
![](https://img.aspxhome.com/file/2023/0/128850_0s.png)
解决eclipse上传svn忽略target文件夹的坑
2023-09-12 04:04:23
![](https://img.aspxhome.com/file/2023/5/58445_0s.jpg)
Java毕业设计实战之平行志愿管理系统的实现
2023-09-12 14:34:23
![](https://img.aspxhome.com/file/2023/2/74872_0s.jpg)
C#使用游标实现补间函数
2021-10-13 21:59:35
![](https://img.aspxhome.com/file/2023/4/94644_0s.jpg)
Sentinel源码解析入口类和SlotChain构建过程详解
2022-06-14 18:29:50
![](https://img.aspxhome.com/file/2023/2/105972_0s.png)
linux系统 java环境变量的配置方法
2022-12-10 09:34:11
C#实现文件与字符串互转的方法详解
2023-04-26 03:18:12
![](https://img.aspxhome.com/file/2023/2/70402_0s.png)
SpringBoot配置log4j输出日志的案例讲解
2023-07-30 18:54:50
详解Android中Intent的使用方法
2023-08-02 04:40:29
![](https://img.aspxhome.com/file/2023/2/122082_0s.png)
C#函数out多个返回值问题
2022-11-14 21:58:55
![](https://img.aspxhome.com/file/2023/0/102290_0s.png)
c#创建圆形类Circle、矩形类实现代码
2023-08-18 19:59:12
![](https://img.aspxhome.com/file/2023/3/80123_0s.png)
C#实现上位机与欧姆龙PLC通讯(FINS)
2022-10-05 03:10:08
Android开发中软键盘的显示和隐藏
2023-10-03 06:00:20
![](https://img.aspxhome.com/file/2023/3/138003_0s.jpg)
C#控制台实现飞行棋游戏
2022-03-12 04:18:40
![](https://img.aspxhome.com/file/2023/2/94642_0s.jpg)