C # Implementing the Express API interface call method

Source: Internet
Author: User
Tags urlencode

No platform restrictions, relying on the Express API network interface

  ----------------entity class [DataContract] public class Syncresponseentity {public syncresponseentity () {}///&LT;SU  Mmary>////</summary> [DataMember (Order = 0, Name = "id")] public string ID {get; set; }///<summary>/////</summary> [DataMember (Order = 1, name = "name")] public str    ing Name {get; set;} <summary>//////</summary> [DataMember (order = 2, Name = "order")] public string O    Rder {get; set;} <summary>///message content///</summary> [DataMember (Order = 5, Name = "message")] public string Mess    Age {get; set;} <summary>//Server Status///</summary> [DataMember (Order = 6, Name = "Errcode")] public string ERR    Code {get; set;}  <summary>//Waybill status///</summary> [DataMember (Order = 7, Name = "status")] public int Status { Get Set }///<summary>//Tracking Records//</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;}    }---------Call method 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>////Synchronous Tracking number method///</summary>//<typeparam name= "T" ></typeparam>//&lt ;p Aram Name= "id" ></param>///<param name= "order" ></param>//<param name= "Issign" ></ Param>    <param name= "Islast" ></param>//<param name= "defaultvalue" ></param>//<return                       s></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"; The string url = Sync_url + string. Format ("? Uid={0}&key;={1}&id;={2}&order;={3}&time;={4}", UID, Currkey, ID, order, HTT        Putility.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 MD5CRYPTOSERVICEP      Rovider (); 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 = Configurationm Anager.      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 SE Rializer = 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.Seria Lization. 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.Seriali Zation.        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 ser      Ializer = 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>///Convert the URL class to a text string POST request///</summary>//<param name= "Data" > </to Post param>//<param name= "url" > Destination url</param>//<returns> server response </returns> public static String post_httpwebrequesthtml (String encodingname, string htmlurl, S Tring 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>//interface type///</summary> public enum Apitype {//sync query sync = 1} 

Basically the code is on top. In the Belt

Apply for a UID and you're done.

The above mentioned is the whole content of this article, I hope you can like.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Implementing the Express API interface call method

This address: http://www.paobuke.com/develop/c-develop/pbk23098.html






Related content very useful C # string manipulation class stringhelper.csc# simple way to get the coordinates of the mouse focus position in full screen example C # Implement a method to close a child window without releasing a child window object C # Resolves a file that is resource-intensive and cannot be deleted or modified
instance of Windows C # GUI window program implementation call Google Map Example C # Mail timed bulk tool Atilia Usage example standard high-order functions in C # Functional programming a summary of common naming rules in C #

C # Implementing the Express API interface call method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.