全国天气预报查询接口如何用C#进行调用?

一、什么是全国天气预报查询接口

1、 根据城市查询天气:输入城市可查询未来5天的天气情况,如:温度、湿度、风向等信息。 2、通过城市名称或城市ID查询城市生活指数,如:运动指数、紫外线指数、穿衣指数等等。有些指数,有时可能为空。

二、应用场景

媒体和新闻:新闻网站、移动端,提供给用户持续更新的天气信息。

科研和教育:气象研究机构运用预报数据进行气候模式研究。

三、如何用C#进行调用?

下面我们用阿里云接口为例,具体的C#代码示例如下:

cs 复制代码
接口地址:https://market.aliyun.com/apimarket/detail/cmapi00067359
//using System.IO;
//using System.Text;
//using System.Net;
//using System.Net.Security;
//using System.Security.Cryptography.X509Certificates;

        private const String host = "https://tsweather.market.alicloudapi.com";
        private const String path = "/weather";
        private const String method = "GET";
        private const String appcode = "你自己的AppCode";

        static void Main(string[] args)
        {
            String querys = "city=1";
            String bodys = "";
            String url = host + path;
            HttpWebRequest httpRequest = null;
            HttpWebResponse httpResponse = null;

            if (0 < querys.Length)
            {
                url = url + "?" + querys;
            }

            if (host.Contains("https://"))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
            }
            else
            {
                httpRequest = (HttpWebRequest)WebRequest.Create(url);
            }
            httpRequest.Method = method;
            httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
            if (0 < bodys.Length)
            {
                byte[] data = Encoding.UTF8.GetBytes(bodys);
                using (Stream stream = httpRequest.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }
            try
            {
                httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            }
            catch (WebException ex)
            {
                httpResponse = (HttpWebResponse)ex.Response;
            }

            Console.WriteLine(httpResponse.StatusCode);
            Console.WriteLine(httpResponse.Method);
            Console.WriteLine(httpResponse.Headers);
            Stream st = httpResponse.GetResponseStream();
            StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
            Console.WriteLine(reader.ReadToEnd());
            Console.WriteLine("\n");

        }

        public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }

成功响应:

cs 复制代码
//近5天天气预报情况
{
    "code": 1,
    "msg": "操作成功",
    "data": {
        "city": "北京",
        "realtime": {
            "temperature": "15", //温度
            "humidity": "7", //湿度
            "info": "晴", //天气情况,如:晴、多云
            "wid": "00", //天气标识id,可参考 天气种类列表 接口
            "direct": "北风", //风向
            "power": "2级", //风力
            "aqi": "28" //空气质量指数
        },
        "future": [ //近5天天气情况
            {
                "date": "2022-03-27", // 日期
                "temperature": "6/15℃", //温度,最低温/最高温
                "weather": "晴",
                "wid": {
                    "day": "00",
                    "night": "00"
                },
                "direct": "西南风" //风向
            },
            {
                "date": "2022-03-28",
                "temperature": "8/18℃",
                "weather": "多云",
                "wid": {
                    "day": "01",
                    "night": "01"
                },
                "direct": "南风"
            },
            {
                "date": "2022-03-29",
                "temperature": "5/11℃",
                "weather": "小雨",
                "wid": {
                    "day": "07",
                    "night": "07"
                },
                "direct": "东风转东北风"
            },
            {
                "date": "2022-03-30",
                "temperature": "3/14℃",
                "weather": "多云转晴",
                "wid": {
                    "day": "01",
                    "night": "00"
                },
                "direct": "东北风转西风"
            },
            {
                "date": "2022-03-31",
                "temperature": "2/16℃",
                "weather": "晴",
                "wid": {
                    "day": "00",
                    "night": "00"
                },
                "direct": "西北风转东北风"
            }
        ]
    }
}
相关推荐
csdn_aspnet9 小时前
使用 MongoDB.Driver 在 C# .NETCore 中实现 Mongo DB 过滤器
mongodb·c#·.netcore
csdn_aspnet14 小时前
使用 C# .NETCore 实现MongoDB
mongodb·c#·.netcore
上位机付工14 小时前
上位机通信速度有多快?
开发语言·c#·上位机·plc
FuckPatience18 小时前
C# 修改基类List中某一元素的子类类型
c#·list
玉面小君21 小时前
从 WPF 到 Avalonia 的迁移系列实战篇6:ControlTheme 和 Style区别
c#·wpf·avalonia
_oP_i1 天前
WinForms 项目里生成时选择“首选目标平台 32 位导致有些电脑在获取office word对象时获取不到
c#·office
要记得喝水1 天前
C#某公司面试题(含题目和解析)--1
开发语言·windows·面试·c#·.net
冷冷的菜哥1 天前
ASP.NET Core文件分片上传
c#·asp.net·asp.net core·文件分片上传