C#获取中国天气网免费天气预报信息
中国天气网接口地址:”http://wthrcdn.etouch.cn/WeatherApi?citykey=” + weatherCityCode(为城市code);
下面是转化过程中我们需要用到的方法(序列化的实体类在文章结尾附)
string weatherInfoUrl = "http://wthrcdn.etouch.cn/WeatherApi?citykey=" + weatherCityCode;
string weatherstr = getHtml2(weatherInfoUrl);
resp tempInfo = XmlDeSeralizer<resp>(weatherstr);
转化过程中需要用到的方法
private static string GetHtml(string url)
{
StringBuilder s = new StringBuilder(102400);
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
HttpWebResponse response = (HttpWebResponse)wr.GetResponse();
Head(response);
GZipStream g = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);
byte[] d = new byte[20480];
int l = g.Read(d, 0, 20480);
while (l > 0)
{
s.Append(Encoding.UTF8.GetString(d, 0, l));
l = g.Read(d, 0, 20480);
}
return s.ToString();
} private static void Head(HttpWebResponse r)
{
string[] keys = r.Headers.AllKeys;
for (int i = 0; i < keys.Length; ++i)
{
Console.WriteLine(keys[i] + " " + r.Headers[keys[i]]);
}
} public static T XmlDeSeralizer<T>(string xmlStr) where T : class,new()
{
XmlSerializer xs = new XmlSerializer(typeof(T));
using (StringReader reader = new StringReader(xmlStr))
{
return xs.Deserialize(reader) as T;
}
}
天气实体类
public class resp
{
public string city { get; set; }
public string updatetime { get; set; }
public string wendu { get; set; }
public string fengli { get; set; }
public string shidu { get; set; }
public string fengxiang { get; set; }
public environment environment { get; set; }
public alarm alarm { get; set; }
public List<weather> forecast { set; get; }
}
public class environment
{
public string aqi { get; set; }
public string pm25 { get; set; }
public string suggest { get; set; }
public string quality { get; set; }
public string MajorPollutants { get; set; }
public string time { get; set; }
}
public class alarm
{
public string cityName { get; set; }
public string alarmType { get; set; }
public string alarmDegree { get; set; }
public string alarmText { get; set; }
public string alarm_details { get; set; }
public string standard { get; set; }
public string suggest { get; set; }
}
public class weather
{
public string date { get; set; }
public string high { get; set; }
public string low { get; set; }
public climate day { get; set; }
public climate night { get; set; }
}
public class climate
{
public string type { get; set; }
public string fengxiang { get; set; }
public string fengli { get; set; }
}
C#获取中国天气网免费天气预报信息的更多相关文章
- python3抓取中国天气网不同城市7天、15天实时数据
思路:1.根据city.txt文档来获取不同城市code2.获取中国天气网7d和15d不同城市url3.利用requests库请求url获取html内容4.利用beautifulsoup获取7d和15 ...
- 中国天气网-天气预报接口api
中国天气网地址:http://www.weather.com.cn 请求服务 : 查询实时天气信息 http://www.weather.com.cn/data/sk/101110101.html 在 ...
- 天气预报接口api(中国天气网)
中国天气weather.comhttp://m.weather.com.cn/data/101110101.html(六天预报) http://www.weather.com.cn/data/sk/1 ...
- C#实现中国天气网XML接口测试
点击链接查看中国天气网接口说明,最近想研究一下接口测试,源于最近一次和某公司的技术总监(交大校友)谈话,发现接口测试的需求是比较大的,于是想要研究一下. 好不容易在网上找到了一个关于中国天气网的接口说 ...
- [转载]中国天气网API
最近在做个网站要用到天气网的api,在网上找了些参考资料,这篇文章对天气网api的介绍比较详细,所以转载之,谢谢原作者的辛勤劳动和奉献精神. 原文地址:http://g.kehou.com/t1033 ...
- 中国天气网API接口
http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data/cityinfo/101010100.h ...
- Android解析中国天气网的Json数据
在Android开发中.一般的APP都是通过获取server端的数据来更新UI.从server获取到的数据能够是Json.它的数据量要比XML要小,这里解析中国天气网上获取的数据,尽管已经不再更新了. ...
- a中国天气网pi(json格式)
http://m.weather.com.cn/data/101050101.html 此接口的回报格式例如以下 { "weatherinfo": { "city&quo ...
- 中国天气网API
中国天气网有三个 API 适用于不同场合的使用. http://m.weather.com.cn/data/101050101.html 这个接口返回的格式如下. { "weatherinf ...
随机推荐
- 使用IntelliJ IDEA和Eclipse导入Github项目
使用IntelliJ IDEA导入Github项目 菜单栏File->Settings->分别配置好Github和Git的相关信息,配置完之后点击旁边的Test按键可以查看是否配置成功 配 ...
- HDU 4768 Flyer【二分】||【异或】
<题目链接> <转载于 >>> > 题目链接: n个社团派发传单,有a,b,c三个参数,派发的规则是,派发给序号为a,a+c....a+k*c,序号要求是小 ...
- 004.Ceph块设备基础使用
一 基础准备 参考<002.Ceph安装部署>文档部署一个基础集群: 新增节点主机名及IP在deploy节点添加解析: [root@deploy ~]# echo "172.24 ...
- C# 正规则表达式
获取括号里的内容 public string GetRegexStr(string Str, string Symbol1, string Symbol2, bool needSymbol) { ]; ...
- 使用百度的富文本编辑器UEditor遇到的问题总结
1.下载地址:http://ueditor.baidu.com/website/download.html(我下载的是.net版本) 下载后要先看一下ueditor.config.js和 net/co ...
- ajax中的async属性值之同步和异步及同步和异步区别
jquery中ajax方法有个属性async用于控制同步和异步,默认是true,即ajax请求默认是异步请求,有时项目中会用到AJAX同步.这个同步的意思是当JS代码加载到当前AJAX的时候会把页面里 ...
- [ZJOI2012]波浪
Description: L = | P2 – P1 | + | P3 – P2 | + - + | PN – PN-1 | 给你一个N和M,问:随机一个1-N的排列,它的波动强度(L)不小于M的概率 ...
- c++中static变量有什么用
主要有两点用途. 1.让一个变量长期有效,而不管其是在什么地方被申明.比如: int fun1() { static int s_value = 0; .... } 那么fun1不管在什么地方被调用, ...
- (转)为什么wait(),notify()和notifyAll()必须在同步块或同步方法中调用
我们常用wait(),notify()和notifyAll()方法来进行线程间通信.线程检查一个条件后就行进入等待状态,例如,在“生产者-消费者”模型中,生产者线程发现缓冲区满了就等待,消费者线程通过 ...
- svn window下过滤文件(如配置文件等)
第一种: 在资源管理器中,右键一个未加入版本控制文件或目录,并从弹出菜单选择TortoiseSVN →Add to Ignore List,会出现一个子菜单,允许你仅选择该文件或者所有具有相同后缀的文 ...