C#_Express-ickd接口
爱查快递接口使用
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Net; 5 using System.Text; 6 using System.Linq; 7 using Newtonsoft.Json; 8 9 namespace ClassLibrary 10 { 11 //快递查询 12 public static class Express 13 { 14 #region ickd.com快递查询接口Key和快递公司 15 private const string ickdKEY = ""; 16 private const string ickdSecret = ""; 17 private static Dictionary<string, string> ickdComDic = new Dictionary<string, string>() { 18 {"顺丰快递","shunfeng"}, 19 {"申通快递","shentong"},{"圆通快递","yuantong"},{"中通快递","zhongtong"},{"韵达快递","yunda"}, 20 {"大田物流","datian"},{"德邦物流","debang"},{"D速快递","dsu"},{"EMS快递","ems"},{"华宇物流","huayu"}, 21 {"汇通快递","huitong"},{"佳吉快运","jiaji"},{"京广快递","jingguang"},{"龙邦快递","longbang"}, 22 {"天天快递","tiantian"},{"新邦物流","xinbang"},{"优速快递","yousu"},{"宅急送快递","zhaijisong"} 23 }; 24 /// <summary> 25 /// 获取快递公司集合 26 /// </summary> 27 public static List<string> idkdGetCom = ickdComDic.Keys.ToList<string>(); 28 #endregion 29 30 #region 使用ickd.com接口获取快递信息 31 /// <summary> 32 /// 查询快递 33 /// </summary> 34 /// <param name="com">快递</param> 35 /// <param name="nu">快递单号</param> 36 /// <param name="order">排序顺序</param> 37 /// <param name="type">返回值类型</param> 38 /// <param name="encode">编码方式</param> 39 /// <returns></returns> 40 public static ickdModel GetICKDModel(string com, string nu, ickdOrderType order = ickdOrderType.desc, ickdDateType type = ickdDateType.json, ickdEncodeType encode = ickdEncodeType.gbk) 41 { 42 try 43 { 44 if (!ickdComDic.ContainsKey(com)) { return null; }//判断快递公司是否合法 45 StringBuilder sb = new StringBuilder(); 46 sb.AppendFormat("http://api.ickd.cn/?id={0}&secret={1}&com={2}&nu={3}&type={4}&ord={5}&encode={6}", ickdKEY, ickdSecret, ickdComDic[com], nu, type, order, encode); 47 48 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sb.ToString());//向url所在的服务器发送一个请求 49 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //获得服务器的回应 50 string charset = response.CharacterSet; 51 Encoding reEncode = Encoding.GetEncoding(charset); 52 TextReader text = new StreamReader(response.GetResponseStream(), reEncode);//初始化流 53 string htmlContent = text.ReadToEnd();//获得网页内容 54 response.Close(); 55 56 ickdModel model = JsonConvert.DeserializeObject<ickdModel>(htmlContent); 57 return model.status != 0 ? model : null; 58 } 59 catch (Exception) 60 { 61 return null; 62 } 63 } 64 #endregion 65 } 66 67 #region ickd枚举参数 68 public enum ickdDateType 69 { 70 json, html, text, xml 71 } 72 public enum ickdOrderType 73 { 74 desc, asc 75 } 76 public enum ickdEncodeType 77 { 78 gbk, utf8 79 } 80 #endregion 81 82 #region 快递信息 83 public class ickdModel 84 { 85 /// <summary> 86 /// 查询结果状态[0查询失败/1正常/2派送中/3已签收/4退回/5其他问题] 87 /// </summary> 88 public int status { get; set; } 89 /// <summary> 90 /// 错误消息 91 /// </summary> 92 public string message { get; set; } 93 /// <summary> 94 /// 进度 95 /// </summary> 96 public List<ickdInfoModel> data { get; set; } 97 /// <summary> 98 /// 快递单号 99 /// </summary> 100 public string mailNo { get; set; } 101 /// <summary> 102 /// 快递公司英文代码 103 /// </summary> 104 public string expSpellName { get; set; } 105 /// <summary> 106 /// 快递公司中文名 107 /// </summary> 108 public string expTextName { get; set; } 109 /// <summary> 110 /// 最后更新时间 111 /// </summary> 112 public string update { get; set; } 113 /// <summary> 114 /// 缓存时间 115 /// </summary> 116 public string cache { get; set; } 117 /// <summary> 118 /// 排序 119 /// </summary> 120 public string ord { get; set; } 121 122 } 123 #endregion 124 125 #region 快递处理信息 126 public class ickdInfoModel 127 { 128 /// <summary> 129 /// 处理时间 130 /// </summary> 131 public string time { get; set; } 132 /// <summary> 133 /// 处理信息 134 /// </summary> 135 public string context { get; set; } 136 } 137 #endregion 138 }
如果现在不努力,以后会活的更累吧。