全国天气预报查询接口如何用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": "西北风转东北风"
            }
        ]
    }
}
相关推荐
偶尔的鼠标人1 天前
Avalonia DataGrid 控件的LostFocus事件会多次触发
开发语言·c#
ytttr8731 天前
C# 仿QQ聊天功能实现 (SQL Server数据库)
数据库·oracle·c#
future_studio1 天前
聊聊 Unity(小白专享、C# 小程序 之 图片播放器)
unity·小程序·c#
万邦科技Lafite1 天前
深度解析:电商API的核心功能与应用
api·开放api·电商开放平台·淘宝开放平台
c#上位机1 天前
wpf中Grid的MouseDown 事件无法触发的原因
c#·wpf
CodeCraft Studio1 天前
国产化PDF处理控件Spire.PDF教程:如何在 C# 中从 HTML 和 PDF 模板生成 PDF
pdf·c#·html·.net·spire.pdf·pdf文档开发·html创建模板pdf
ysdysyn1 天前
.NET 10深度解析:性能革新与开发生态的全新篇章
c#·.net
L X..2 天前
Unity 光照贴图异常修复笔记
unity·c#·游戏引擎
reasonsummer2 天前
【办公类-115-06】20250920职称资料上传04——docx复制、docx转PDF(课程表11个)
开发语言·windows·python·c#
用户268001379192 天前
干货!Python采集淘宝商品详情数据,API接口(json数据返回)
api