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

基于C#的电视台节目表接口调用代码

程序员文章站 2023-11-08 15:27:46
接口地址: using system; using system.collections.generic; using system.linq; using...

接口地址:

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, 0, 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> parameters, 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();
      }
    }
  }
}