Program table interface calling code _c# tutorials based on C #

Source: Internet
Author: User

Interface Address: 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; ----------------------------------//TV program schedule call sample code-aggregation data//Online interface Documentation: HTTP://WWW.JUHE.CN/DOCS/129// Jsonobject class Download address in code: http://download.csdn.net/download/gcm3206021155665/7458439//------------------------------- ---namespace Consoleapi {class Program {static void Main (string[] args) {string appkey = "******** ***********";
 
      Configure your requested Appkey//1. TV station Classification String URL1 = "Http://japi.juhe.cn/tv/getCategory";
 
      var parameters1 = new dictionary<string, string> (); Parameters1.
 
      ADD ("Key", appkey);//You apply for the key string result1 = Sendpost (URL1, parameters1, "get");
      Jsonobject newObj1 = new Jsonobject (RESULT1); String errorCode1 = newobj1["Error_code"].
 
      Value;
        if (errorCode1 = = "0") {Debug.WriteLine ("successful"); DebuG.writeline (NEWOBJ1);
        else {//debug.writeline ("failed"); Debug.WriteLine (newobj1["Error_code"). Value+ ":" +newobj1["Reason"].
      Value);
 
      //2. TV Channel list string url2 = "Http://japi.juhe.cn/tv/getChannel";
 
      var parameters2 = new dictionary<string, string> (); Parameters2. ADD ("Key", appkey);//You apply for the key parameters2. ADD ("PId", "");
 
      TV Category ID String result2 = Sendpost (Url2, Parameters2, "get");
      Jsonobject newObj2 = new Jsonobject (RESULT2); String ErrorCode2 = newobj2["Error_code"].
 
      Value;
        if (ErrorCode2 = = "0") {Debug.WriteLine ("successful");
      Debug.WriteLine (NEWOBJ2);
        else {//debug.writeline ("failed"); Debug.WriteLine (newobj2["Error_code"). Value+ ":" +newobj2["Reason"].
      Value);
 
      //3. TV program Single List string url3 = "Http://japi.juhe.cn/tv/getProgram";
 
      var parameters3 = new dictionary<string, string> (); Parameters3. ADD ("key", appkey);//The key parameters3 you applied for. ADD ("Code", ""); Channel code PARAMETERS3. ADD ("date", "");
 
      Date (format yyyy-mm-dd, default for today's date) string result3 = Sendpost (Url3, Parameters3, "get");
      Jsonobject newObj3 = new Jsonobject (RESULT3); String errorCode3 = newobj3["Error_code"].
 
      Value;
        if (errorCode3 = = "0") {Debug.WriteLine ("successful");
      Debug.WriteLine (NEWOBJ3);
        else {//debug.writeline ("failed"); Debug.WriteLine (newobj3["Error_code"). Value+ ":" +newobj3["Reason"].
      Value); }///<summary>///Http (get/post)///</summary>///<param name= "url" > Request u rl</param>///<param name= "parameters" > Request parameters </param>///<param name= "method" > Request </par am>///<returns> Response content </returns> static string sendpost (string url, idictionary<string, String&gt ; 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 {//create requests HttpWebRequest request = (HttpWebRequest) webrequest.create (url + "?" +
 
        Buildquery (Parameters, "UTF8")); Get requested 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"));
        Returns the content string retstring = Mystreamreader.readtoend ();
      return retstring;
    }///<summary>///assemble normal text request parameters. </summary>///<param name= "parameters" >key-value form request parameter Dictionary </param>///<returns>url encoded
      Request data </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; Parameters that omit the parameter name or null argument 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>///Converts the response flow to text.   </summary>///<param name= "RSP" > Response stream object </param>///<param name= "Encoding" > Encoding method </ param>///<returns> Response text </returns> static string getresponseasstring (HttpWebResponse RSP, Encoding
      Encoding) {System.IO.Stream Stream = null;
      StreamReader reader = null; try {//Read the HTTP response stream = RSP in the form of character streams.
        GetResponseStream ();
        reader = new StreamReader (stream, encoding); Return reader.
      ReadToEnd (); finally {//release resource if (reader!= null) reader.
        Close (); if (stream!= null) stream.
        Close (); if (RSP!= null) RSP.
      Close ();
 }
    }
  }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.