wp8.1 调用中国天气api

在调用api应用的过程,我们需要用hmac加密技术,它是一种基于hash的加密算法,通过一个双方共同约定的密钥,在发送message前,对密钥进行了sha散列计算,在生成消息又对此密钥进行了二次加密,通过周期性的更换密钥,安全性可以得到保障。

在wp8.1 sdk中很多传统系统类库被整编进以windows打头的命名空间中,很多刚接触wp8.1朋友可能觉得疑惑。

代码所需命名空间。

1 using System;
2 using System.Net.Http;
3 using System.Threading.Tasks;
4 using Windows.Security.Cryptography;
5 using Windows.Security.Cryptography.Core;
6 using Windows.Storage.Streams;

加密方法。

 1 public string CreateHMAC(string publicKey, string privateKey)
 2 {
 3     string strAlgName = MacAlgorithmNames.HmacSha1;
 4     MacAlgorithmProvider objMacProv = MacAlgorithmProvider.OpenAlgorithm(strAlgName);
 5
 6     IBuffer data = CryptographicBuffer.ConvertStringToBinary(publicKey, BinaryStringEncoding.Utf8);
 7     IBuffer buffKeyMaterial = CryptographicBuffer.ConvertStringToBinary(privateKey, BinaryStringEncoding.Utf8);
 8     CryptographicKey hmacKey = objMacProv.CreateKey(buffKeyMaterial);
 9
10     var buffHMAC = CryptographicEngine.Sign(hmacKey, data);
11
12     return CryptographicBuffer.EncodeToBase64String(buffHMAC);
13 }

调用方式,根据天气网api得知,加密的message为除去key之外的url,再根据私钥签名加密,得到最终的key。

 1 public MainPageViewModel()
 2 {
 3     GetAsync();
 4 }
 5
 6 public async void GetAsync()
 7 {
 8     string areaId = "101010100";
 9     string type = "observe";
10     string date = System.DateTime.Now.ToString("yyyyMMddHHmm");
11     string appId = "---天气网提供的appid---";
12     string privateKey = "---天气网提供的key---";
13     string publicKey = string.Format("http://open.weather.com.cn/data/?areaid={0}&type={1}&date={2}&appid={3}",
14         areaId, type, date, appId);
15
16     string key = CreateHMAC(publicKey, privateKey);
17     string url = string.Format(@"http://open.weather.com.cn/data/?areaid={0}&type={1}&date={2}&appid={3}&key={4}",
18         areaId, type, date, appId.Substring(0, 6), key);
19
20     var httpClient = new HttpClient();
21     var content = await httpClient.GetStringAsync(url);
22     await Task.Run(() =>
23     {
24         string c = content;
25     });
26 }

返回的结果就是一串json了。

wp8.1 调用中国天气api

时间: 2024-08-29 15:53:51

wp8.1 调用中国天气api的相关文章

JAVA调用聚合天气api接口示例

查询天气预报在APP中常用的一个常用功能,聚合数据免费天气api接口可以根据根据城市名/id查询天气.根据IP查询天气.据GPS坐标查询天气.查询城市天气三小时预报,并且支持全国不同城市天气预报查询. 代码描述:基于JAVA的免费天气api接口调用示例,根据文档中注明的需求参数,调用接口返回数据. 关联数据:免费天气api 接口地址:https://www.juhe.cn/docs/api/id/39 step1:选择本文所示例的接口"免费天气api" url:https://www.

模块一、调用中国天气网和qqOnline及TrainTimeWebService接口来突出Json方法

一. 浏览网页的时候,发送的请求.服务器反回来的永远是字符串,由于服务器后台使用的语言不通,所以就需要用工具反解,这里用到了json json方法一 json.loads()将字符串转化为python的基本数据类型(内部必须是双引号) 中国天气网城市代码网络接口来 代码实现: 1 req = urllib.request.Request("http://www.weather.com.cn/adat/sk/101010300.html") 2 r = urllib.request.ur

使用python调用和风天气API查询当前天气信息

和风天气网址:https://www.heweather.com/ 注册后在控制台会有个人认证key API帮助文档:https://www.heweather.com/documents/api/v5 #!/usr/bin/python #-*-coding:utf-8-*- #调用和风天气API查询当前天气信息 #2017/07/12 import json import urllib2 import os city='北京' #在和风天气注册后获得的key key='your key' c

天气预报接口api(中国天气网)

中国天气weather.comhttp://m.weather.com.cn/data/101110101.html(六天预报) http://www.weather.com.cn/data/sk/101110101.html(实时天气信息) 其中101110101是城市的代码,获得城市代码进入 http://www.weather.com.cn在搜索框上输入你要需要获得天气的城市,点击查询,即可在地址栏获得相应城市编号,然后替换http://m.weather.com.cn/data/1011

中国天气网API无法更新

很多人发现中国天气网API无法更新了,以为是收费了或者咋滴(GQ真的都太不负责任了,什么通知都不给我们) 百度了下,发现API地址变更了: 天气预报:http://m.weather.com.cn/atad/101230201.html 实时天气:http://mobile.weather.com.cn/data/sk/101230201.html 中国天气网API无法更新

中国天气网api

namespace wyl.Weather { /// <summary> /// 天气预报辅助类 /// 接口数据来自于 中国天气网 /// </summary> public class WeatherHelper { private static readonly string appId = ConfigurationManager.AppSettings["appId"].ToString(); private static readonly stri

中国天气网-天气预报接口api

中国天气网地址:http://www.weather.com.cn 请求服务 : 查询实时天气信息 http://www.weather.com.cn/data/sk/101110101.html 在浏览器中输入请求地址,获得西安的天气. 其中101110101是城市的代码,如果要查询其他城市的天气,只需要修改城市的代码即可,在中国天气网中城市代码如下: 101010100=北京101010200=海淀101010300=朝阳101010400=顺义101010500=怀柔101010600=通

天气API接口大全(nohacks.cn 收集整理)

自序: 由nohacks.cn 收集整理,来源于网络,版权归原作者所有,基本收集了网络上能使用的大部分天气API接口,作者水平精力有限,难免有遗漏或错误的地方,欢迎反馈,作者网站:http://nohacks.cn 更新记录: 2015.3.2    更新云聚,增加天气网接口. 2015.2.28  增加百度天气图片,感谢网友彬子的反馈. 2015. 2.19 增加百度天气接口,更新中国天气网API. 目录: 1.  中国天气网 1.1  实时天气 API 接口(失效) 1.2  当天天气 AP

【第29篇】通过HttpClient+Gson解析中国天气网的天气预报信息

http://jilongliang.iteye.com/blog/1932110 package com.test; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; im