C# 接入 SmartWeatherAPI

实现效果

1.填写发送申请信息
API地址:http://smart.weather.com.cn/wzfw/smart/weatherapi.shtml

2.等待通过审核,审核周期一至三周
回复邮件如下:


回复邮件
回复邮件

3.根据官方文档、以及前辈代码,编写调用示例

代码示例

public string GetKey(string appid, string privateKey, string areaId, string date, string type)
    {
        //使用SHA1的HMAC
        HMAC hmac = HMACSHA1.Create();
        var publicKey = "http://open.weather.com.cn/data/?areaid={0}&type={1}&date={2}&appid={3}";
        var data = System.Text.Encoding.UTF8.GetBytes(string.Format(publicKey, areaId, type, date, appid));
        //密钥
        var key = System.Text.Encoding.UTF8.GetBytes(privateKey);
        hmac.Key = key;
        //对数据进行签名
        var signedData = hmac.ComputeHash(data);
        string encodeKey = Server.UrlEncode(Convert.ToBase64String(signedData));
        return string.Format(publicKey, areaId, type, date, appid.Substring(0, 6)) + "&key=" + encodeKey;
    }

    public string GetWeather(string url)
    {
        string strBuff = "";
        char[] cbuffer = new char[256];
        int byteRead = 0;

        Uri httpURL = new Uri(url);
        HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(httpURL);

        HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
        Stream respStream = httpResp.GetResponseStream();
        StreamReader respStreamReader = new StreamReader(respStream, Encoding.UTF8);
        byteRead = respStreamReader.Read(cbuffer, 0, 256);
        while (byteRead != 0)
        {
            string strResp = new string(cbuffer, 0, byteRead);
            strBuff = strBuff + strResp;
            byteRead = respStreamReader.Read(cbuffer, 0, 256);
        }

        respStream.Close();
        return strBuff;
}

    protected void Page_Load(object sender, EventArgs e)
    {
        
        string appid = "*******";
        string private_key = "****************";
        string address = "101010100";
        string type = "forecast3d";     //type:数据类型(实况:observe,指数:index,常规预告:forecast3d)。
        string url = GetKey(appid, private_key, address, DateTime.Now.ToString("yyyyMMddHHmm"), type);
        strWeather = GetWeather(url);

    }

注意事项

1、官方API地址变更,之前的地址请求无法返回数据请求
错误地址

http:// webapi.weather.com.cn/data/?areaid={0}&type={1}&date={2}&appid={3}

正确地址

http://open.weather.com.cn/data/?areaid={0}&type={1}&date={2}&appid={3}

2、Type参数不正确

type:数据类型(实况:observe,指数:index,常规预告:forecast3d)

3、参数截取不正确

http://open.weather.com.cn/data/?areaid={0}&type={1}&date={2}&appid={3}&key={4}
areaid为行政区划代码(见邮件附件)
type类型分实况:observe,指数:index,常规预告:forecast3d
date为请求时间,格式为yyyyMMddHHmm
appid为 Appid前6位字符(邮件中提供Appid)
key通过Appid,privateKey加密计算(邮件中提供Appid,privateKey)

参考资料

薛定谔の喵
Jolan

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,359评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,270评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,200评论 4 61
  • 杜荀鹤《溪兴》原诗、注释、翻译、赏析 【原文】:溪兴杜荀鹤山雨溪风卷钓丝, 瓦瓯篷底独斟时。醉来睡着无人唤, 流到...
    xcy无名阅读 2,900评论 0 0
  • 一切为了谁 作词:卢木鸟 作曲: 覃艺馨 一一一献给驻村第一书记们 放弃城市工作驻农村 谁把贫困户当亲人 风雨寒雪...
    華狐发布阅读 2,463评论 0 0