java调用api接口实例 java调用api接口实例 基于C#的最新影讯接口调用代码实例

基于C#的最新影讯接口调用代码实例

代码描述:基于C#的最新影讯接口调用代码实例

代码平台:聚合数据

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/94

// 代码中JsonObject类下载地址:http://download.csdn.net/download/gcm3206021155665/7458439

//----------------------------------

namespace ConsoleAPI

{

class Program

{

static void Main(string[] args)

{

string appkey = "*******************"; //配置您申请的appkey

//1.影视搜索

string url1 = "http://op.juhe.cn/onebox/movie/video";

var parameters1 = new Dictionary<string, string>();

parameters1.Add("key", appkey);//你申请的key

parameters1.Add("dtype" , ""); //返回数据的格式,xml或json,默认json

parameters1.Add("q" , ""); //影视搜索名称

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://op.juhe.cn/onebox/movie/pmovie";

var parameters2 = new Dictionary<string, string>();

parameters2.Add("key", appkey);//你申请的key

parameters2.Add("dtype" , ""); //返回数据的格式,xml或json,默认json

parameters2.Add("city" , ""); //城市名称

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);

}

}

/// <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>

java调用api接口实例 java调用api接口实例 基于C#的最新影讯接口调用代码实例

/// 组装普通文本请求参数。

/// </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(); }

}

}

}

  

爱华网本文地址 » http://www.aihuau.com/a/397551/767880057400.html

更多阅读

C#虚函数抽象函数接口虚函数的运用 虚函数和抽象函数

虚函数(virtual):可由子类继承并重写的函数,后期绑定抽像函数(abstract):规定其非虚子类必须实现的函数,必须被重写(override实现)。抽象类不能被实例化,抽象方法必须是空方法,由派生类去实现它(重载)接口(interface):必须重写(实现接口必须重写接口中

基于端口划分VLAN实例 端口划分vlan

通过本实训可以学习到:基于端口划分VLAN的方法;查看VLAN划分情况。基于端口划分VLAN实例——工具/原料1台Cisco1912交换机4台PC机,4条双绞线1台带有超级终端程序的PC机,以及Console电缆及转接器。基于端口划分VLAN实例——步骤/方

如何使用JpcapCaptor实例来捕捉网络数据包 截获网络数据包

当你通过打开网卡设备得到JpcapCaptor实例后,就可以利用它来捕捉网络数据包了。你可以利用以下两种方法来捕捉网络数据包。第一种是通过回调函数的机制,另一种是利用JpcapCaptor实例的getPacket()的方法,一个数据包一个数据包的接收。

声明:《java调用api接口实例 java调用api接口实例 基于C#的最新影讯接口调用代码实例》为网友很傻很固执分享!如侵犯到您的合法权益请联系我们删除