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 ...
随机推荐
- POJ1062昂贵的聘礼(经典) 枚举区间 +【Dijkstra】
<题目链接> 昂贵的聘礼 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用1000 ...
- unity3d俄罗斯方块源码教程+源码和程序下载
小时候,大家都应玩过或听说过<俄罗斯方块>,它是红白机,掌机等一些电子设备中最常见的一款游戏.而随着时代的发展,信息的进步,游戏画面从简单的黑白方块到彩色方块,游戏的玩法机制从最简单的消方 ...
- Effective Java 第三版——59. 熟悉并使用Java类库
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- RCNN系列超详细解析
一.基于Region Proposal(候选区域)的深度学习目标检测算法 Region Proposal(候选区域),就是预先找出图中目标可能出现的位置,通过利用图像中的纹理.边缘.颜色等信息,保证在 ...
- 在Node.js中在保持目录结构的情况下压缩指定目录
最近在做一个文件升级的功能,需要从下载服务器中指定目录下的文件.在学习了zlib后发现这个模块达不到这个功能 在查找资料后发现后发现 archiver 模块很好用,不过我也发现大部分中文资料没有如果查 ...
- java 实验 三 (1)
(1)金字塔:Pyramid.java在屏幕上显示一个由星型符号“*”组成的金字塔图案,要求用户设置金字塔的高度,程序能根据用户设置的高度打印金字塔,示例如下: * ******** package ...
- (转)HashMap底层实现原理/HashMap与HashTable区别/HashMap与HashSet区别
①HashMap的工作原理 HashMap基于hashing原理,我们通过put()和get()方法储存和获取对象.当我们将键值对传递给put()方法时,它调用键对象的hashCode()方法来计算h ...
- AVL树的理解及自写AVL树
AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增加和删除可能需要通过一次或多 ...
- BZOJ2702 : 金融风暴
求出离每个点最近的关键点,然后用二维ST表回答正方形最大值. 将关键点分为上下两部分,以上为例: 从上到下依次考虑每一行,记录每一列往上最近的关键点的距离,那么最优决策具有单调性,可以分治求解. 时间 ...
- vim技巧4 删除/保留文本中匹配行
vim技巧:如何删除/保留文本中特定的行呢? <ol><a href="/ss/ss/www"> show invisibles</a> < ...