欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于C#的電視臺節(jié)目表接口調(diào)用代碼

 更新時間:2016年06月19日 18:04:36   投稿:hebedich  
這篇文章主要介紹了基于C#的電視臺節(jié)目表接口調(diào)用代碼的相關(guān)資料,需要的朋友可以參考下

接口地址: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;
 
//----------------------------------
// 電視節(jié)目時間表調(diào)用示例代碼 - 聚合數(shù)據(jù)
// 在線接口文檔: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.電視臺節(jié)目單列表
      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" , ""); //日期(格式y(tǒng)yyy-MM-dd,默認為當(dāng)天日期)
 
      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">請求參數(shù)</param>
    /// <param name="method">請求方法</param>
    /// <returns>響應(yīng)內(nèi)容</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
      {
        //創(chuàng)建請求
        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"));
 
        //返回內(nèi)容
        string retString = myStreamReader.ReadToEnd();
        return retString;
      }
    }
 
    /// <summary>
    /// 組裝普通文本請求參數(shù)。
    /// </summary>
    /// <param name="parameters">Key-Value形式請求參數(shù)字典</param>
    /// <returns>URL編碼后的請求數(shù)據(jù)</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;
        // 忽略參數(shù)名或參數(shù)值為空的參數(shù)
        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>
    /// 把響應(yīng)流轉(zhuǎn)換為文本。
    /// </summary>
    /// <param name="rsp">響應(yīng)流對象</param>
    /// <param name="encoding">編碼方式</param>
    /// <returns>響應(yīng)文本</returns>
    static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
    {
      System.IO.Stream stream = null;
      StreamReader reader = null;
      try
      {
        // 以字符流的方式讀取HTTP響應(yīng)
        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();
      }
    }
  }
}

相關(guān)文章

  • C# this關(guān)鍵字的四種用法

    C# this關(guān)鍵字的四種用法

    這篇文章主要為大家詳細介紹了C# this關(guān)鍵字的四種用法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • C#設(shè)計模式之Singleton模式

    C#設(shè)計模式之Singleton模式

    這篇文章主要介紹了C#設(shè)計模式中的Singleton模式相關(guān)知識,文中代碼非常詳細,供大家理解學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • c#通過xpath讀取xml示例

    c#通過xpath讀取xml示例

    這篇文章主要介紹了c#通過xpath讀取xml示例,需要的朋友可以參考下
    2014-04-04
  • C#寫日志類實例

    C#寫日志類實例

    這篇文章主要介紹了C#寫日志類,實現(xiàn)將日志信息寫入文本文件的功能,非常具有實用價值,需要的朋友可以參考下
    2014-10-10
  • C#判斷一天、一年已經(jīng)過了百分之多少的方法

    C#判斷一天、一年已經(jīng)過了百分之多少的方法

    這篇文章主要介紹了C#判斷一天、一年已經(jīng)過了百分之多少的方法,涉及C#針對時間及日期的運算與判定技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • C#中Parallel類For、ForEach和Invoke使用介紹

    C#中Parallel類For、ForEach和Invoke使用介紹

    這篇文章介紹了C#中Parallel類For、ForEach和Invoke的使用方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • c#的sortedlist使用方法

    c#的sortedlist使用方法

    這篇文章主要介紹了c#的sortedlist使用方法,需要的朋友可以參考下
    2014-05-05
  • C#數(shù)組排序的兩種常用方法

    C#數(shù)組排序的兩種常用方法

    這篇文章主要介紹了C#數(shù)組排序的兩種常用方法,實例分析了C#操作數(shù)組的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • Repeater中添加按鈕實現(xiàn)點擊按鈕獲取某一行數(shù)據(jù)的方法

    Repeater中添加按鈕實現(xiàn)點擊按鈕獲取某一行數(shù)據(jù)的方法

    這篇文章主要介紹了Repeater中添加按鈕實現(xiàn)點擊按鈕獲取某一行數(shù)據(jù)的方法,是非常實用的一個技巧,需要的朋友可以參考下
    2014-08-08
  • C#反射使用方法過程及步驟

    C#反射使用方法過程及步驟

    這篇文章主要為大家詳細介紹了C#反射使用方法過程及步驟,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09

最新評論