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

发布于:2025-06-12 ⋅ 阅读:(49) ⋅ 点赞:(0)

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

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

二、应用场景

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

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

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

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

接口地址: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;
        }

成功响应:

//近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": "西北风转东北风"
            }
        ]
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到