基于c#的节假日日历接口调用代码实例_第1页
基于c#的节假日日历接口调用代码实例_第2页
基于c#的节假日日历接口调用代码实例_第3页
基于c#的节假日日历接口调用代码实例_第4页
基于c#的节假日日历接口调用代码实例_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于 C#的节假日日历接口调用代码实例 代码描述:基于 C#的节假日日历接口调用代码实例 代码平台:聚合数据 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using Xfrog.Net; using System.Diagnostics; using System.Web; /- / 万年历调用示例代码 聚合数据 / 在线接口文档:/docs/177 / 代码中 JsonObject 类下载地址:/download/gcm32060 21155665/7458439 /- namespace ConsoleAPI class Program static void Main(string args) string appkey = “*“; /配置您申请的 appkey /1.获取当天的详细信息 string url1 = “/calendar/day“; var parameters1 = new Dictionary(); parameters1.Add(“key“, appkey);/你申请的 key parameters1.Add(“date“ , “); /指定日期,格式为 YYYY-MM-DD,如 月份和日期小于 10,则取个位,如:2012-1-1 string result1 = sendPost(url1, parameters1, “get“); JsonObject newObj1 = new JsonObject(result1); String errorCode1 = newObj1“error_code“.Value; if (errorCode1 = “0“) Debug.WriteLine(“成功“); Debug.WriteLine(newObj1); else /Debug.WriteLine(“失败“); Debug.WriteLine(newObj1“error_code“.Value+“:“+newObj1 “reason“.Value); /2.获取当月近期假期 string url2 = “/calendar/month“; var parameters2 = new Dictionary(); parameters2.Add(“key“, appkey);/你申请的 key parameters2.Add(“year-month“ , “); /指定月份,格式为 YYYY-MM, 如月份和日期小于 10,则取个位,如:2012-1 string result2 = sendPost(url2, parameters2, “get“); JsonObject newObj2 = new JsonObject(result2); String errorCode2 = newObj2“error_code“.Value; if (errorCode2 = “0“) Debug.WriteLine(“成功“); Debug.WriteLine(newObj2); else /Debug.WriteLine(“失败“); Debug.WriteLine(newObj2“error_code“.Value+“:“+newObj2 “reason“.Value); /3.获取当年的假期列表 string url3 = “/calendar/year“; var parameters3 = new Dictionary(); parameters3.Add(“key“, appkey);/你申请的 key parameters3.Add(“year“ , “); /指定年份,格式为 YYYY,如:2015 string result3 = sendPost(url3, parameters3, “get“); JsonObject newObj3 = new JsonObject(result3); String errorCode3 = newObj3“error_code“.Value; if (errorCode3 = “0“) Debug.WriteLine(“成功“); Debug.WriteLine(newObj3); else /Debug.WriteLine(“失败“); Debug.WriteLine(newObj3“error_code“.Value+“:“+newObj3 “reason“.Value); / / Http (GET/POST) / / 请求 URL / 请求参数 / 请求方法 / 响应内容 static string sendPost(string url, IDictionary p arameters, string method) if (method.ToLower() = “post“) HttpWebRequest req = null; HttpWebResponse rsp = null; System.IO.Stream reqStream = null; try req = (HttpWebRequest)WebRequest.Create(url); req.Method = method; req.KeepAlive = false; req.ProtocolVersion = HttpVersion.Version10; req.Timeout = 5000; req.ContentType = “application/x-www-form-urlencoded;c harset=utf-8“; byte postData = Encoding.UTF8.GetBytes(BuildQuery( parameters, “utf8“); reqStream = req.GetRequestStream(); reqStream.Write(postData, 0, postData.Length); rsp = (HttpWebResponse)req.GetResponse(); Encoding encoding = Encoding.GetEncoding(rsp.Charact erSet); return GetResponseAsString(rsp, encoding); catch (Exception ex) return ex.Message; finally if (reqStream != null) reqStream.Close(); if (rsp != null) rsp.Close(); else /创建请求 HttpWebRequest request = (HttpWebRequest)WebRequest.Crea te(url + “?“ + BuildQuery(parameters, “utf8“); /GET 请求 request.Method = “GET“; request.ReadWriteTimeout = 5000; request.ContentType = “text/html;charset=UTF-8“; HttpWebResponse response = (HttpWebResponse)request.GetR esponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myRespons eStream, Encoding.GetEncoding(“utf-8“); /返回内容 string retString = myStreamReader.ReadToEnd(); return retString; / / 组装普通文本请求参数。 / / Key-Value 形式请求参数字典 / URL 编码后的请求数据 static string BuildQuery(IDictionary parameters, string encode) StringBuilder postData = new StringBuilder(); bool hasParam = false; IEnumerator dem = parameters.G etEnumerator(); while (dem.MoveNext() string name = dem.Current.Key; string value = dem.Current.Value; / 忽略参数名或参数值为空的参数 if (!string.IsNullOrEmpty(name)/ postData.Append(name); postData.Append(“=“); if (encode = “gb2312“) postData.Append(HttpUtility.UrlEncode(value, Enc oding.GetEncoding(“gb2312“); else if (encode = “utf8“) postData.Append(HttpUtility.UrlEncode(value, Enc oding.UTF8); else postData.Append(value); hasParam = true; return postData.ToString(); / / 把响应流转换为文本。 / / 响应流对象 / 编码方式 / 响应文本 static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding) System.IO.Stream stream = null; StreamReader reader = null; try / 以字符流的方式读取 HTT

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论