接口地址:http://www.juhe.cn/docs/api/id/129
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; //----------------------------------// 电视节目时间表调用示例代码 - 聚合数据// 在线接口文档:http://www.juhe.cn/docs/129// 代码中JsonObject类下载地址:http://download.csdn.net/download/gcm3206021155665/7458439//---------------------------------- namespace ConsoleAPI{ class Program { static voID Main(string[] args) { string appkey = "*******************"; //配置您申请的appkey //1.电视台分类 string url1 = "http://jAPI.juhe.cn/tv/getcategory"; var parameters1 = new Dictionary<string,string>(); parameters1.Add("key",appkey);//你申请的key 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 = "http://jAPI.juhe.cn/tv/getChannel"; var parameters2 = new Dictionary<string,string>(); parameters2.Add("key",appkey);//你申请的key parameters2.Add("pID",""); //电视分类ID 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 = "http://jAPI.juhe.cn/tv/getProgram"; var parameters3 = new Dictionary<string,string>(); parameters3.Add("key",appkey);//你申请的key parameters3.Add("code",""); //频道代码 parameters3.Add("date",""); //日期(格式yyyy-MM-dd,默认为当天日期) 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); } } /// <summary> /// http (GET/POST) /// </summary> /// <param name="url">请求URL</param> /// <param name="parameters">请求参数</param> /// <param name="method">请求方法</param> /// <returns>响应内容</returns> static string sendPost(string url,IDictionary<string,string> parameters,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;charset=utf-8"; byte[] postData = EnCoding.UTF8.GetBytes(Buildquery(parameters,"utf8")); reqStream = req.GetRequestStream(); reqStream.Write(postData,postData.Length); rsp = (httpWebResponse)req.GetResponse(); EnCoding enCoding = EnCoding.GetEnCoding(rsp.CharacterSet); 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.Create(url + "?" + Buildquery(parameters,"utf8")); //GET请求 request.Method = "GET"; request.ReaDWriteTimeout = 5000; request.ContentType = "text/HTML;charset=UTF-8"; httpWebResponse response = (httpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream,EnCoding.GetEnCoding("utf-8")); //返回内容 string retString = myStreamReader.ReadToEnd(); return retString; } } /// <summary> /// 组装普通文本请求参数。 /// </summary> /// <param name="parameters">Key-Value形式请求参数字典</param> /// <returns>URL编码后的请求数据</returns> static string Buildquery(IDictionary<string,string encode) { StringBuilder postData = new StringBuilder(); bool hasParam = false; IEnumerator<keyvaluePair<string,string>> dem = parameters.GetEnumerator(); while (dem.MoveNext()) { string name = dem.Current.Key; string value = dem.Current.Value; // 忽略参数名或参数值为空的参数 if (!string.IsNullOrEmpty(name))//&& !string.IsNullOrEmpty(value) { if (hasParam) { postData.Append("&"); } postData.Append(name); postData.Append("="); if (encode == "gb2312") { postData.Append(httpUtility.UrlEncode(value,EnCoding.GetEnCoding("gb2312"))); } else if (encode == "utf8") { postData.Append(httpUtility.UrlEncode(value,EnCoding.UTF8)); } else { postData.Append(value); } hasParam = true; } } return postData.ToString(); } /// <summary> /// 把响应流转换为文本。 /// </summary> /// <param name="rsp">响应流对象</param> /// <param name="enCoding">编码方式</param> /// <returns>响应文本</returns> static string GetResponseAsstring(httpWebResponse rsp,EnCoding enCoding) { System.IO.Stream stream = null; StreamReader reader = null; try { // 以字符流的方式读取http响应 stream = rsp.GetResponseStream(); reader = new StreamReader(stream,enCoding); return reader.ReadToEnd(); } finally { // 释放资源 if (reader != null) reader.Close(); if (stream != null) stream.Close(); if (rsp != null) rsp.Close(); } } }}总结
以上是内存溢出为你收集整理的基于C#的电视台节目表接口调用代码全部内容,希望文章能够帮你解决基于C#的电视台节目表接口调用代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)