基于C#的天气预报接口调用代码实例
代码描述:基于C#的天气预报接口调用代码实例 代码平台:聚合数据
usingSystem;
usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Net; usingSystem.IO; usingXfrog.Net;
usingSystem.Diagnostics; usingSystem.Web;
//---------------------------------- // 天气预报调用示例代码 - 聚合数据
// 在线接口文档:http://www.juhe.cn/docs/73
// 代码中JsonObject类下载地址:http://download.csdn.net/download/gcm3206021155665/7458439
//----------------------------------
namespaceConsoleAPI {
classProgram {
staticvoidMain(string[] args) {
stringappkey = \配置您申请的appkey
//1.根据城市查询天气
stringurl1 = \http://op.juhe.cn/onebox/weather/query\
var parameters1 = newDictionary
parameters1.Add(\要查询的城市,如:温州、上海、北京
parameters1.Add(\你申请的key
parameters1.Add(\返回数据的格式,xml或json,默认json
stringresult1 = sendPost(url1, parameters1, \
JsonObject newObj1 = newJsonObject(result1); String errorCode1 = newObj1[\
if(errorCode1 == \ {
Debug.WriteLine(\成功\ Debug.WriteLine(newObj1); } else {
//Debug.WriteLine(\失败\
Debug.WriteLine(newObj1[\+newObj1[\ }
}
///
/// Http (GET/POST) ///
///
/// /// ///
staticstringsendPost(stringurl, IDictionary
if(method.ToLower() == \ {
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 = \rm-urlencoded;charset=utf-8\
byte[] postData = Encoding.UTF8.GetBytes(BuildQuery(parameters, \
reqStream = req.GetRequestStream(); reqStream.Write(postData, 0, postData.Length);
rsp = (HttpWebResponse)req.GetResponse();
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
returnGetResponseAsString(rsp, encoding);
}
catch(Exception ex) {
returnex.Message; }
finally {
if(reqStream != null) reqStream.Close();
if(rsp != null) rsp.Close(); } } else {
//创建请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + \
//GET请求
request.Method = \
request.ReadWriteTimeout = 5000;
request.ContentType = \\
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = newStreamReader(myResponseStream, Encoding.GetEncoding(\
//返回内容
stringretString = myStreamReader.ReadToEnd(); returnretString; } }
///
/// 组装普通文本请求参数。 ///
/// ///
staticstringBuildQuery(IDictionary
StringBuilder postData = newStringBuilder(); boolhasParam = false;
IEnumerator
while(dem.MoveNext()) {
stringname = dem.Current.Key; stringvalue = dem.Current.Value; // 忽略参数名或参数值为空的参数
if(!string.IsNullOrEmpty(name))//&& !string.IsNullOrEmpty(value)
{
if(hasParam) {
postData.Append(\ }
postData.Append(name); postData.Append(\ if(encode == \ {
postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding(\ }
elseif(encode == \ {
postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
} else {
postData.Append(value);
}
}
hasParam = true; } }
returnpostData.ToString();
///
/// 把响应流转换为文本。 ncoding)
} }
///
/// /// ///
staticstringGetResponseAsString(HttpWebResponse rsp, Encoding e{ System.IO.Stream stream = null; StreamReader reader = null; try {
// 以字符流的方式读取HTTP响应 stream = rsp.GetResponseStream();
reader = newStreamReader(stream, encoding); returnreader.ReadToEnd(); }
finally {
// 释放资源
if(reader != null) reader.Close(); if(stream != null) stream.Close(); if(rsp != null) rsp.Close(); }
}