首页 万年历 万年历示例代码 节日查询[C#]

节日查询示例代码C#

作者: 阅读数:1121 上传时间:2025-04-23

节日查询

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

class Calendar
{
    private const string AppKey = "YOUR_APPKEY_HERE";
   
    // 查询节假日信息
    public static async Task HolidayAsync()
    {
        try
        {
            using var client = new HttpClient();
            string url = "https://api.jisuapi.com/calendar/holiday";
            string requestUrl = $"{url}?appkey={AppKey}";

            HttpResponseMessage response = await client.GetAsync(requestUrl);
            response.EnsureSuccessStatusCode();

            string responseBody = await response.Content.ReadAsStringAsync();
            JObject jsonarr = JObject.Parse(responseBody);

            if ((int)jsonarr["status"] != 0)
            {
                Console.WriteLine(jsonarr["msg"]);
                return;
            }

            JObject result = (JObject)jsonarr["result"];
            foreach (var item in result)
            {
                string key = item.Key;
                JObject value = (JObject)item.Value;
                Console.WriteLine($"{key} : {value["content"]} {value["name"]}");
            }
        }
        catch (HttpRequestException ex)
        {
            Console.WriteLine($"请求出错: {ex.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生错误: {ex.Message}");
        }
    }
    static async Task Main(string[] args)
    {
        await Calendar.QueryAsync("2024-01-01");
        await Calendar.HolidayAsync();  
    }
}