![](http://static.blog.csdn.net/images/category_icon.jpg)
版权声明:本文为博主原创文章,未经博主允许不得转载。
#region 获取快递100提供的查询快递信息的函数
/// <summary>
/// 获取快递100提供的查询快递信息的函数
/// 调用示例:
/// Dictionary<string, string> kuaidi100 = StaticFunApp.GetKuaidi100("优速物流", "669009108318");
///foreach (KeyValuePair<string,string> en in kuaidi100)
///{
///Response.WriteLine(en.Key+" "+en.Value);
///}
/// </summary>
/// <param name="typeCom">物流公司</param>
/// <param name="epxno">快递单号</param>
/// <returns></returns>
public static Dictionary<string, string> GetKuaidi100(string typeCom, string epxno)
{
#region 获取快递信息
string ApiKey = "XXXXXX";//请把XXXXXX修改成您在快递100网站申请的APIKey
//string powered = "快递数据由: <a href=\"http://www.kuaidi100.com/?refer=hishop&a=e8bc69faf6f1270b\" target=\"_blank\">快递100</a> 提供";
//技术文档地址:http://code.google.com/p/kuaidi-api/wiki/Open_API_API_URL
Dictionary<string, string> dict = new Dictionary<string, string>();
switch (typeCom)
{
case "AAE全球专递":
typeCom = "aae";
break;
case "安捷快递":
typeCom = "anjiekuaidi";
break;
case "安信达快递":
typeCom = "anxindakuaixi";
break;
case "百福东方":
typeCom = "baifudongfang";
break;
case "彪记快递":
typeCom = "biaojikuaidi";
break;
case "BHT":
typeCom = "bht";
break;
case "希伊艾斯快递":
typeCom = "cces";
break;
case "中国东方":
typeCom = "Coe";
break;
case "长宇物流":
typeCom = "changyuwuliu";
break;
case "大田物流":
typeCom = "datianwuliu";
break;
case "德邦物流":
typeCom = "debangwuliu";
break;
case "DPEX":
typeCom = "dpex";
break;
case "DHL":
typeCom = "dhl";
break;
case "D速快递":
typeCom = "dsukuaidi";
break;
case "fedex":
typeCom = "fedex";
break;
case "飞康达物流":
typeCom = "feikangda";
break;
case "凤凰快递":
typeCom = "fenghuangkuaidi";
break;
case "港中能达物流":
typeCom = "ganzhongnengda";
break;
case "广东邮政物流":
typeCom = "guangdongyouzhengwuliu";
break;
case "汇通快运":
typeCom = "huitongkuaidi";
break;
case "恒路物流":
typeCom = "hengluwuliu";
break;
case "华夏龙物流":
typeCom = "huaxialongwuliu";
break;
case "佳怡物流":
typeCom = "jiayiwuliu";
break;
case "京广速递":
typeCom = "jinguangsudikuaijian";
break;
case "急先达":
typeCom = "jixianda";
break;
case "佳吉物流":
typeCom = "jiajiwuliu";
break;
case "加运美":
typeCom = "jiayunmeiwuliu";
break;
case "快捷速递":
typeCom = "kuaijiesudi";
break;
case "联昊通物流":
typeCom = "lianhaowuliu";
break;
case "龙邦物流":
typeCom = "longbanwuliu";
break;
case "民航快递":
typeCom = "minghangkuaidi";
break;
case "配思货运":
typeCom = "peisihuoyunkuaidi";
break;
case "全晨快递":
typeCom = "quanchenkuaidi";
break;
case "全际通物流":
typeCom = "quanjitong";
break;
case "全日通快递":
typeCom = "quanritongkuaidi";
break;
case "全一快递":
typeCom = "quanyikuaidi";
break;
case "盛辉物流":
typeCom = "shenghuiwuliu";
break;
case "速尔物流":
typeCom = "suer";
break;
case "盛丰物流":
typeCom = "shengfengwuliu";
break;
case "天地华宇":
typeCom = "tiandihuayu";
break;
case "天天快递":
typeCom = "tiantian";
break;
case "TNT":
typeCom = "tnt";
break;
case "UPS":
typeCom = "ups";
break;
case "万家物流":
typeCom = "wanjiawuliu";
break;
case "文捷航空速递":
typeCom = "wenjiesudi";
break;
case "伍圆速递":
typeCom = "wuyuansudi";
break;
case "万象物流":
typeCom = "wanxiangwuliu";
break;
case "新邦物流":
typeCom = "xinbangwuliu";
break;
case "信丰物流":
typeCom = "xinfengwuliu";
break;
case "星晨急便":
typeCom = "xingchengjibian";
break;
case "鑫飞鸿物流":
typeCom = "xinhongyukuaidi";
break;
case "亚风速递":
typeCom = "yafengsudi";
break;
case "一邦速递":
typeCom = "yibangwuliu";
break;
case "优速物流":
typeCom = "youshuwuliu";
break;
case "远成物流":
typeCom = "yuanchengwuliu";
break;
case "圆通速递":
typeCom = "yuantong";
break;
case "源伟丰快递":
typeCom = "yuanweifeng";
break;
case "元智捷诚快递":
typeCom = "yuanzhijiecheng";
break;
case "越丰物流":
typeCom = "yuefengwuliu";
break;
case "韵达快递":
typeCom = "yunda";
break;
case "源安达":
typeCom = "yuananda";
break;
case "运通快递":
typeCom = "yuntongkuaidi";
break;
case "宅急送":
typeCom = "zhaijisong";
break;
case "中铁快运":
typeCom = "zhongtiewuliu";
break;
case "中通速递":
typeCom = "zhongtong";
break;
case "中邮物流":
typeCom = "zhongyouwuliu";
break;
default:
dict.Add("result",typeCom+" is not supported");
return dict;
}
string CacheKey = typeCom + "-" + epxno;//作为一个缓存
object cacheobj = DataCache.GetCache(CacheKey);
string detail = "";
if (cacheobj == null)
{
string apiurl = "http://api.kuaidi100.com/api?id=" + ApiKey + "&com=" + typeCom + "&nu=" + epxno + "&show=1&muti=1&order=asc";
//参数show String 返回类型。0:返回json字符串,1:返回xml对象,2:返回html对象,3:返回text文本。如果不填,默认返回json字符串
//参数muti Stirng 返回信息数量,1:返回多行完整的信息,0:只返回一行信息。不填默认返回多行
//参数order Stirng 排序。desc:按时间由新到旧排列,asc:按时间由旧到新排列。不填默认返回倒序(大小不敏感)
//Response.Write (apiurl);
WebRequest request = WebRequest.Create(@apiurl);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
Encoding encode = Encoding.UTF8;
StreamReader reader = new StreamReader(stream, encode);
detail = reader.ReadToEnd();
DataCache.SetCacheSecond(CacheKey, detail, 30, TimeSpan.Zero);//查询后设置缓存
}
else detail = cacheobj.IsNull();
#endregion
#region 用xml解析获取的信息
string kuaidi100 = detail;
XmlDocument doc = new XmlDocument();
doc.InnerXml = kuaidi100;
string message = doc.SelectSingleNode("xml/message").InnerText;
//state---0:在途中, 1:已发货, 2:疑难件, 3:已签收, 4:已退货。
string state = doc.SelectSingleNode("xml/state").InnerText;
switch (state)
{
case "0":
state = "在途中";
break;
case "1":
state = "已发货";
break;
case "2":
state = "疑难件";
break;
case "3":
state = "已签收";
break;
case "4":
state = "已退货";
break;
}
//status---0:运单暂无结果, 1:查询成功, 2:接口出现异常,408:验证码出错(仅适用于APICode url) 。
string status = doc.SelectSingleNode("xml/status").InnerText;
switch (status)
{
case "0":
status = "运单暂无结果";
break;
case "1":
status = "查询成功";
break;
case "2":
status = "接口出现异常";
break;
case "408":
status = "验证码出错";
break;
}
dict.Add("status", status);
dict.Add("state", state);
if (status == "查询成功")
{
XmlNodeList list = doc.SelectNodes("xml/data");
foreach (XmlNode node in list)
{
string time = node.SelectSingleNode("time").InnerText;//节点下的子节点名称
string context = node.SelectSingleNode("context").InnerText;//节点下的子节点名称
dict.Add(time, context);
}
}
#endregion
return dict;
}
#endregion
/// <summary>
/// 获取快递100提供的查询快递信息的函数
/// 调用示例:
/// Dictionary<string, string> kuaidi100 = StaticFunApp.GetKuaidi100("优速物流", "669009108318");
///foreach (KeyValuePair<string,string> en in kuaidi100)
///{
///Response.WriteLine(en.Key+" "+en.Value);
///}
/// </summary>
/// <param name="typeCom">物流公司</param>
/// <param name="epxno">快递单号</param>
/// <returns></returns>
public static Dictionary<string, string> GetKuaidi100(string typeCom, string epxno)
{
#region 获取快递信息
string ApiKey = "XXXXXX";//请把XXXXXX修改成您在快递100网站申请的APIKey
//string powered = "快递数据由: <a href=\"http://www.kuaidi100.com/?refer=hishop&a=e8bc69faf6f1270b\" target=\"_blank\">快递100</a> 提供";
//技术文档地址:http://code.google.com/p/kuaidi-api/wiki/Open_API_API_URL
Dictionary<string, string> dict = new Dictionary<string, string>();
switch (typeCom)
{
case "AAE全球专递":
typeCom = "aae";
break;
case "安捷快递":
typeCom = "anjiekuaidi";
break;
case "安信达快递":
typeCom = "anxindakuaixi";
break;
case "百福东方":
typeCom = "baifudongfang";
break;
case "彪记快递":
typeCom = "biaojikuaidi";
break;
case "BHT":
typeCom = "bht";
break;
case "希伊艾斯快递":
typeCom = "cces";
break;
case "中国东方":
typeCom = "Coe";
break;
case "长宇物流":
typeCom = "changyuwuliu";
break;
case "大田物流":
typeCom = "datianwuliu";
break;
case "德邦物流":
typeCom = "debangwuliu";
break;
case "DPEX":
typeCom = "dpex";
break;
case "DHL":
typeCom = "dhl";
break;
case "D速快递":
typeCom = "dsukuaidi";
break;
case "fedex":
typeCom = "fedex";
break;
case "飞康达物流":
typeCom = "feikangda";
break;
case "凤凰快递":
typeCom = "fenghuangkuaidi";
break;
case "港中能达物流":
typeCom = "ganzhongnengda";
break;
case "广东邮政物流":
typeCom = "guangdongyouzhengwuliu";
break;
case "汇通快运":
typeCom = "huitongkuaidi";
break;
case "恒路物流":
typeCom = "hengluwuliu";
break;
case "华夏龙物流":
typeCom = "huaxialongwuliu";
break;
case "佳怡物流":
typeCom = "jiayiwuliu";
break;
case "京广速递":
typeCom = "jinguangsudikuaijian";
break;
case "急先达":
typeCom = "jixianda";
break;
case "佳吉物流":
typeCom = "jiajiwuliu";
break;
case "加运美":
typeCom = "jiayunmeiwuliu";
break;
case "快捷速递":
typeCom = "kuaijiesudi";
break;
case "联昊通物流":
typeCom = "lianhaowuliu";
break;
case "龙邦物流":
typeCom = "longbanwuliu";
break;
case "民航快递":
typeCom = "minghangkuaidi";
break;
case "配思货运":
typeCom = "peisihuoyunkuaidi";
break;
case "全晨快递":
typeCom = "quanchenkuaidi";
break;
case "全际通物流":
typeCom = "quanjitong";
break;
case "全日通快递":
typeCom = "quanritongkuaidi";
break;
case "全一快递":
typeCom = "quanyikuaidi";
break;
case "盛辉物流":
typeCom = "shenghuiwuliu";
break;
case "速尔物流":
typeCom = "suer";
break;
case "盛丰物流":
typeCom = "shengfengwuliu";
break;
case "天地华宇":
typeCom = "tiandihuayu";
break;
case "天天快递":
typeCom = "tiantian";
break;
case "TNT":
typeCom = "tnt";
break;
case "UPS":
typeCom = "ups";
break;
case "万家物流":
typeCom = "wanjiawuliu";
break;
case "文捷航空速递":
typeCom = "wenjiesudi";
break;
case "伍圆速递":
typeCom = "wuyuansudi";
break;
case "万象物流":
typeCom = "wanxiangwuliu";
break;
case "新邦物流":
typeCom = "xinbangwuliu";
break;
case "信丰物流":
typeCom = "xinfengwuliu";
break;
case "星晨急便":
typeCom = "xingchengjibian";
break;
case "鑫飞鸿物流":
typeCom = "xinhongyukuaidi";
break;
case "亚风速递":
typeCom = "yafengsudi";
break;
case "一邦速递":
typeCom = "yibangwuliu";
break;
case "优速物流":
typeCom = "youshuwuliu";
break;
case "远成物流":
typeCom = "yuanchengwuliu";
break;
case "圆通速递":
typeCom = "yuantong";
break;
case "源伟丰快递":
typeCom = "yuanweifeng";
break;
case "元智捷诚快递":
typeCom = "yuanzhijiecheng";
break;
case "越丰物流":
typeCom = "yuefengwuliu";
break;
case "韵达快递":
typeCom = "yunda";
break;
case "源安达":
typeCom = "yuananda";
break;
case "运通快递":
typeCom = "yuntongkuaidi";
break;
case "宅急送":
typeCom = "zhaijisong";
break;
case "中铁快运":
typeCom = "zhongtiewuliu";
break;
case "中通速递":
typeCom = "zhongtong";
break;
case "中邮物流":
typeCom = "zhongyouwuliu";
break;
default:
dict.Add("result",typeCom+" is not supported");
return dict;
}
string CacheKey = typeCom + "-" + epxno;//作为一个缓存
object cacheobj = DataCache.GetCache(CacheKey);
string detail = "";
if (cacheobj == null)
{
string apiurl = "http://api.kuaidi100.com/api?id=" + ApiKey + "&com=" + typeCom + "&nu=" + epxno + "&show=1&muti=1&order=asc";
//参数show String 返回类型。0:返回json字符串,1:返回xml对象,2:返回html对象,3:返回text文本。如果不填,默认返回json字符串
//参数muti Stirng 返回信息数量,1:返回多行完整的信息,0:只返回一行信息。不填默认返回多行
//参数order Stirng 排序。desc:按时间由新到旧排列,asc:按时间由旧到新排列。不填默认返回倒序(大小不敏感)
//Response.Write (apiurl);
WebRequest request = WebRequest.Create(@apiurl);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
Encoding encode = Encoding.UTF8;
StreamReader reader = new StreamReader(stream, encode);
detail = reader.ReadToEnd();
DataCache.SetCacheSecond(CacheKey, detail, 30, TimeSpan.Zero);//查询后设置缓存
}
else detail = cacheobj.IsNull();
#endregion
#region 用xml解析获取的信息
string kuaidi100 = detail;
XmlDocument doc = new XmlDocument();
doc.InnerXml = kuaidi100;
string message = doc.SelectSingleNode("xml/message").InnerText;
//state---0:在途中, 1:已发货, 2:疑难件, 3:已签收, 4:已退货。
string state = doc.SelectSingleNode("xml/state").InnerText;
switch (state)
{
case "0":
state = "在途中";
break;
case "1":
state = "已发货";
break;
case "2":
state = "疑难件";
break;
case "3":
state = "已签收";
break;
case "4":
state = "已退货";
break;
}
//status---0:运单暂无结果, 1:查询成功, 2:接口出现异常,408:验证码出错(仅适用于APICode url) 。
string status = doc.SelectSingleNode("xml/status").InnerText;
switch (status)
{
case "0":
status = "运单暂无结果";
break;
case "1":
status = "查询成功";
break;
case "2":
status = "接口出现异常";
break;
case "408":
status = "验证码出错";
break;
}
dict.Add("status", status);
dict.Add("state", state);
if (status == "查询成功")
{
XmlNodeList list = doc.SelectNodes("xml/data");
foreach (XmlNode node in list)
{
string time = node.SelectSingleNode("time").InnerText;//节点下的子节点名称
string context = node.SelectSingleNode("context").InnerText;//节点下的子节点名称
dict.Add(time, context);
}
}
#endregion
return dict;
}
#endregion