欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-13 10:58:46
无平台限制,依赖于快递api网接口 ----------------实体类 [datacontract] public class syncres...

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

  ----------------实体类
  [datacontract]
  public class syncresponseentity
  {
    public syncresponseentity() { }
    /// <summary>
    /// 需要查询的快递代号
    /// </summary>
    [datamember(order = 0, name = "id")]
    public string id { get; set; }
 
    /// <summary>
    /// 需要查询的快递名称
    /// </summary>
    [datamember(order = 1, name = "name")]
    public string name { get; set; }
 
    /// <summary>
    /// 需要查询的快递单号
    /// </summary>
    [datamember(order = 2, name = "order")]
    public string order { get; set; }
 
    /// <summary>
    /// 消息内容
    /// </summary>
    [datamember(order = 5, name = "message")]
    public string message { get; set; }
 
    /// <summary>
    /// 服务器状态
    /// </summary>
    [datamember(order = 6, name = "errcode")]
    public string errcode { get; set; }
 
    /// <summary>
    /// 运单状态
    /// </summary>
    [datamember(order = 7, name = "status")]
    public int status { get; set; }
 
    /// <summary>
    /// 跟踪记录
    /// </summary>
    [datamember(order = 8, 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 = 0, name = "time")]
    public string time { get; set; }
 
    [datamember(order = 1, name = "content")]
    public string content { get; set; }
 
  }
 
---------调用方法
public static int uid = utils.getappconfig<int>("kuaidiapi_uid", 0);
    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 = 0; 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 = 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;
    }
  }
 
  /// <summary>
  /// 接口类型
  /// </summary>
  public enum apitype
  {
    //同步查询
    sync = 1
  }

基本上代码都在上面。在带上申请一个uid就大功告成。

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

上一篇:

下一篇: