1、基于 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:/ 代码中 JsonObject类下载地址:http:/ ConsoleAPIclass
2、 Programstatic void Main(string args)string appkey = “*“; /配置您申请的 appkey/1.根据城市查询天气string url1 = “http:/ parameters1 = new Dictionary();parameters1.Add(“cityname“ , “); /要查询的城市,如:温州、上海、北京parameters1.Add(“key“, appkey);/你申请的 keyparameters1.Add(“dtype“ , “); /返回数据的格式,xml 或 json,默认 jsonstring result1 =
3、 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);/ / Http
4、(GET/POST)/ / 请求 URL/ 请求参数/ 请求方法/ 响应内容static string sendPost(string url, IDictionary parameters, string method)if (method.ToLower() = “post“)HttpWebRequest req = null;HttpWebResponse rsp = null;System.IO.Stream reqStream = null;tryreq = (HttpWebRequest)WebRequest.Create(url);req.Method = method;req.
5、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);
6、rsp = (HttpWebResponse)req.GetResponse();Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);return GetResponseAsString(rsp, encoding);catch (Exception ex)return ex.Message;finallyif (reqStream != null) reqStream.Close();if (rsp != null) rsp.Close();else/创建请求HttpWebRequest request = (HttpWebR
7、equest)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();St
8、reamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(“utf-8“);/返回内容string retString = myStreamReader.ReadToEnd();return retString;/ / 组装普通文本请求参数。/ / Key-Value形式请求参数字典/ URL编码后的请求数据static string BuildQuery(IDictionary parameters, string encode)StringBuilder postData = ne
9、w StringBuilder();bool hasParam = false;IEnumerator dem = parameters.GetEnumerator();while (dem.MoveNext()string name = dem.Current.Key;string value = dem.Current.Value;/ 忽略参数名或参数值为空的参数if (!string.IsNullOrEmpty(name)/postData.Append(name);postData.Append(“=“);if (encode = “gb2312“)postData.Append(Ht
10、tpUtility.UrlEncode(value, Encoding.GetEncoding(“gb2312“);else if (encode = “utf8“)postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8);elsepostData.Append(value);hasParam = true;return postData.ToString();/ / 把响应流转换为文本。/ / 响应流对象/ 编码方式/ 响应文本static string GetResponseAsString(HttpWebResponse rs
11、p, 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();