全国天气预报查询接口如何用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": "西北风转东北风"
            }
        ]
    }
}
相关推荐
唐青枫2 分钟前
别滥用 Task.Run:C# 异步并发实操指南
c#·.net
我好喜欢你~7 小时前
C#---StopWatch类
开发语言·c#
一阵没来由的风11 小时前
拒绝造轮子(C#篇)ZLG CAN卡驱动封装应用
c#·can·封装·zlg·基础封装·轮子
一枚小小程序员哈17 小时前
基于微信小程序的家教服务平台的设计与实现/基于asp.net/c#的家教服务平台/基于asp.net/c#的家教管理系统
后端·c#·asp.net
Eternity_GQM18 小时前
【Word VBA Zotero 引用宏错误分析与改正指南】【解决[21–23]参考文献格式插入超链接问题】
开发语言·c#·word
cimeo1 天前
【C 学习】06-算法&程序设计举例
c#
百锦再1 天前
.NET 的 WebApi 项目必要可配置项都有哪些?
java·开发语言·c#·.net·core·net
WYH2871 天前
C#控制台输入(Read()、ReadKey()和ReadLine())
开发语言·c#
hqwest1 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
做一位快乐的码农2 天前
基于.net、C#、asp.net、vs的保护大自然网站的设计与实现
c#·asp.net·.net