全国天气预报查询接口如何用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": "西北风转东北风"
            }
        ]
    }
}
相关推荐
William_cl9 小时前
【连载7】 C# MVC 跨框架异常处理对比:.NET Framework 与 .NET Core 实现差异
c#·mvc·.net
ajassi20009 小时前
开源 C# 快速开发(五)自定义控件--仪表盘
开发语言·开源·c#
Dream achiever10 小时前
3.WPF - 依赖属性
c#·wpf
mudtools10 小时前
.NET操作Excel:高级格式设置
c#·.net·excel·wps
温启志c#11 小时前
winform c# 做的2个运控平台,通过修改表格 的方式,也可以通过语音识别的交互方式,更加智能。
开发语言·c#
Dream achiever13 小时前
11.WPF 的命令处理事件--参数介绍
开发语言·c#·wpf
布伦鸽13 小时前
C# WPF DataGrid 数据绑定时的单元格编辑类型模板
开发语言·c#·wpf
入目星河滚烫14 小时前
Unity避坑——继承了MonoBehaviour的对象不能通过new来创建
unity·c#·游戏引擎
大飞pkz14 小时前
【设计模式】组合模式
开发语言·设计模式·c#·组合模式
Aevget15 小时前
DevExpress WPF中文教程:如何使用虚拟源将WPF数据网格绑定到任何数据源?
c#·.net·wpf·界面控件·devexpress·ui开发