一般初学者可以百度“聚合数据”,上面都有大量的接口和实例可供参考。。
以下是用来调用接口的俩类:
/// <summary>
/// 加载Http请求 /// </summary> /// <param name="Url"></param> /// <returns></returns> public static string HttpGet(string Url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "GET"; 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(); myStreamReader.Close(); myResponseStream.Close();return retString;
} /// <summary> /// 接口调用方法 /// </summary> /// <param name="Url"></param> /// <param name="postDataStr"></param> /// <returns></returns> public static string HttpGet(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr); request.Method = "GET"; 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(); myStreamReader.Close(); myResponseStream.Close();return retString;
}
下面是接口调用的具体例子,大家可以借鉴一下:
string url = "http://v.juhe.cn/weather/index?cityname=1066&dtype=json&format=1&key=7be8407858533644414e40a2029baccd";
string Json=IService.HttpGet(url); object jsons = JsonConvert.SerializeObject(Json); Console.WriteLine(jsons); Console.WriteLine("--------------------------------------"); object jsonObj= (JObject)JsonConvert.DeserializeObject(Json);//喜欢的可以关注一下我的博客http://www.cnblogs.com/lizong000/