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