C#实现快递api接口调用方法

2021-07-25 06:03

阅读:667

标签:eve   rem   catch   api   就是   焦点   url   gety   from   

无平台限制,依赖于快递api网接口

  ----------------实体类
  [DataContract]
  public class SyncResponseEntity
  {
    public SyncResponseEntity() { }
    /// 
    /// 需要查询的快递代号
    /// 
    [DataMember(Order = 0, Name = "id")]
    public string ID { get; set; }
    /// 
    /// 需要查询的快递名称
    /// 
    [DataMember(Order = 1, Name = "name")]
    public string Name { get; set; }
    /// 
    /// 需要查询的快递单号
    /// 
    [DataMember(Order = 2, Name = "order")]
    public string Order { get; set; }
    /// 
    /// 消息内容
    /// 
    [DataMember(Order = 5, Name = "message")]
    public string Message { get; set; }
    /// 
    /// 服务器状态
    /// 
    [DataMember(Order = 6, Name = "errcode")]
    public string ErrCode { get; set; }
    /// 
    /// 运单状态
    /// 
    [DataMember(Order = 7, Name = "status")]
    public int Status { get; set; }
    /// 
    /// 跟踪记录
    /// 
    [DataMember(Order = 8, Name = "data")]
    public List 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("KUAIDIAPI_UID", 0);
    public static string sync_url = Utils.GetAppConfig("KUAIDIAPI_SYNC_URL", string.Empty);
    public static string key = Utils.GetAppConfig("KUAIDIAPI_KEY", string.Empty);
    /// 
    /// 同步单号查询方法
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    public static T APIQueryDataSYNC(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}ℴ={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(html, defaultValue);
      }
      catch (Exception ex)
      {
        throw new Exception(ex.Message);
      }
      return defaultValue;
    }
  }
  /// 
  /// 辅助工具类
  /// 
  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}ℴ={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 (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(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 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(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(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 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;
    }
    /// 
    /// 将网址类容转换成文本字符串 post请求
    /// 
    /// 要post的数据
    /// 目标url
    /// 服务器响应
    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;
    }
  }
  /// 
  /// 接口类型
  /// 
  public enum APIType
  {
    //同步查询
    SYNC = 1
  }

基本上代码都在上面。在带

上申请一个uid就大功告成。

以上所述就是本文的全部内容了,希望大家能够喜欢。

除声明外,跑步客文章均为原创,转载请以链接形式标明本文地址
  C#实现快递api接口调用方法

本文地址:  http://www.paobuke.com/develop/c-develop/pbk23098.html




相关内容

技术分享图片
非常实用的C#字符串操作处理类StringHelper.cs
技术分享图片
C#简单获取全屏中鼠标焦点位置坐标的方法示例
技术分享图片
C#实现关闭子窗口而不释放子窗口对象的方法
技术分享图片
C#解决文件被占用资源,无法删除或修改的方法

技术分享图片
Windows下C#的GUI窗口程序中实现调用Google Map的实例
技术分享图片
C#邮件定时群发工具Atilia用法实例
技术分享图片
C#函数式编程中的标准高阶函数详解
技术分享图片
C#常用的命名规则汇总

C#实现快递api接口调用方法

标签:eve   rem   catch   api   就是   焦点   url   gety   from   

原文地址:http://www.cnblogs.com/paobuke/p/8035647.html


评论


亲,登录后才可以留言!