程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 基於C#的電視台節目表接口挪用代碼

基於C#的電視台節目表接口挪用代碼

編輯:C#入門知識

基於C#的電視台節目表接口挪用代碼。本站提示廣大學習愛好者:(基於C#的電視台節目表接口挪用代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是基於C#的電視台節目表接口挪用代碼正文


接口地址: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, 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();
      }
    }
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved