----------------实体类
[DataContract]
public class SyncResponseEntity
{
public SyncResponseEntity() { }
/// <summary>
/// 需要查询的快递代号
/// </summary>
[DataMember(Order = , Name = "id")]
public string ID { get; set; } /// <summary>
/// 需要查询的快递名称
/// </summary>
[DataMember(Order = , Name = "name")]
public string Name { get; set; } /// <summary>
/// 需要查询的快递单号
/// </summary>
[DataMember(Order = , Name = "order")]
public string Order { get; set; } /// <summary>
/// 消息内容
/// </summary>
[DataMember(Order = , Name = "message")]
public string Message { get; set; } /// <summary>
/// 服务器状态
/// </summary>
[DataMember(Order = , Name = "errcode")]
public string ErrCode { get; set; } /// <summary>
/// 运单状态
/// </summary>
[DataMember(Order = , Name = "status")]
public int Status { get; set; } /// <summary>
/// 跟踪记录
/// </summary>
[DataMember(Order = , 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 = , Name = "time")]
public string Time { get; set; } [DataMember(Order = , Name = "content")]
public string Content { get; set; } } ---------调用方法
public static int uid = Utils.GetAppConfig<int>("KUAIDIAPI_UID", );
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 = ; 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 = ;
httpRequest.ReadWriteTimeout = ;
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytesToPost, , 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 =
} 基本上代码都在上面。在带www.kuaidiapi.cn上申请一个uid就大功告成。

快递api网接口快递调用方法的更多相关文章

  1. 齐全的IP地址查询接口及调用方法(转)

    设计蜂巢IP地址查询接口:http://www.hujuntao.com/api/ip/ip.php 腾讯IP地址查询接口:http://fw.qq.com/ipaddress 新浪IP地址查询接口: ...

  2. python接口的调用方法

    第一个图灵机器人接口实例: #!/usr/bin/env python # -*- coding: utf_8 -*- import urllib2,urllib import json import ...

  3. IP地址查询接口及调用方法

    1.查询地址 搜狐IP地址查询接口(IP):http://pv.sohu.com/cityjson 1616 IP地址查询接口(IP+地址):http://w.1616.net/chaxun/ipto ...

  4. java 类中定义接口的调用方法

    public class Human { public interface MyAction { public void getPower(); } } public class Test{ publ ...

  5. 云极知客开放平台接口调用方法(C#)

    云极知客为企业提供基于SAAS的智能问答服务.支持企业个性化知识库的快速导入,借助语义模型的理解和分析,使企业客户立即就拥有本行业的24小时客服小专家.其SAAS模式实现零成本投入下的实时客服数据的可 ...

  6. Saltstack的API接口与调用方式

     saltstack看起来是成为一个大规模自己主动化运维和云计算管理的一个框架,类似于SDK,并非像puppet仅仅成为一个工具.基于良好设计的API和清楚的思路,让salt的二次开发变得非常easy ...

  7. 快递鸟顺丰物流api接口对接多种方法整理

    目前很多自营电商平台.ERP系统.仓储系统.快递柜企业,对物流模块数据需求还是比较旺盛的.之前有介绍过简单的接口对接方法,这次给大家整理介绍两种快递数据的获取方法. 接口秘钥可以向顺丰公司申请,或者一 ...

  8. 快递Api接口 & 微信公众号开发流程

    之前的文章,已经分析过快递Api接口可能被使用的需求及场景:今天呢,简单给大家介绍一下微信公众号中怎么来使用快递Api接口,来完成我们的需求和业务场景. 开发语言:Nodejs,其中用到了Neo4j图 ...

  9. 快递查询api(多接口方案)

    /** 本环境使用php+smarty,结合两种快递api调取快递数据 * 说明,先快递鸟调取数据,失败后再调取快递网的数据* 快递鸟 http://www.kdniao.com 快递网 http:/ ...

随机推荐

  1. 状态压缩 HDU 3182

    t组数据 n个汉堡 e的能量 接下来的2行 val    n个 得到的权 cost  n个 花去的能量 接下来n行 每行一个q  q个数字 代表这类汉堡做好要的前提  每个汉堡只能用一次 #inclu ...

  2. 跨域http请求

    <?php header("Access-Control-Allow-Origin: *"); header("Content-Type: application/ ...

  3. Eclipse设置不格式化注释

    Eclipse设置不格式化注释 注释中写点带格式的文字,format后全乱了,解决办法如下: Windows -> Preferces -> java -> Code Style - ...

  4. 算法与数据结构之顺序查找(C语言)

    #include<stdio.h> #include<stdlib.h> //顺序查找基本思想:从线性表的一端开始,逐个检查关键字是否满足给定的条件 int Sequentia ...

  5. jsp动态include和静态Include

    动态 INCLUDE 用 jsp:include 动作实现 <jsp:include page="included.jsp" flush="true" / ...

  6. python-generator生成杨辉三角

    根据廖雪峰老师的评论区摘录. 1: def triangles(): L = [1] while True: yield L L1 = [0] + L[:] L = [L[i]+L1[i] for i ...

  7. Matlab中cell存储为txt

    clc clear all [data1,data3]=textread('E:\RSWeb\mahoyt数据集\movielens\u.user','%s%*d%s%*s%*s','delimite ...

  8. bzoj1103: [POI2007]大都市meg

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1103 题目大意:在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Ma ...

  9. (转载)最长递增子序列 O(NlogN)算法

    原博文:传送门 最长递增子序列(Longest Increasing Subsequence) 下面我们简记为 LIS. 定义d[k]:长度为k的上升子序列的最末元素,若有多个长度为k的上升子序列,则 ...

  10. linux快速安装lamp环境

    我折腾过不少的lamp安装方式,lnmp也折腾过.不过因为windows下面apache用的比较多,各种配置也比较熟悉,因此最终还是选择了lamp这个架构. 由于是自己装虚拟机玩,所以各种安全措施都没 ...