首页 车辆尾号限行 车辆尾号限行示例代码 城市限行查询[C#]

城市限行查询示例代码C#

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

城市限行查询

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

class VehicleLimit
{
    private const string AppKey = "YOUR_APPKEY_HERE"; // 替换为实际的 appkey

    // 查询指定城市和日期的限行信息
    public static async Task QueryAsync(string city, string date)
    {
        try
        {
            using var client = new HttpClient();
            string url = $"https://api.jisuapi.com/vehiclelimit/query?appkey={AppKey}&city={city}&date={date}";

            HttpResponseMessage response = await client.GetAsync(url);
            response.EnsureSuccessStatusCode();
  
            string responseContent = await response.Content.ReadAsStringAsync();
            JObject jsonarr = JObject.Parse(responseContent);
      
            int status = jsonarr["status"]?.ToObject() ?? -1;   
            JObject? result = jsonarr["result"] as JObject;         
            Console.WriteLine(result["time"]?.ToString() ?? "");
            string timestr = "";
            if (result["time"] is JArray timeArray)
            {
                foreach (var time in timeArray)
                {
                    timestr += " " + (time?.ToString() ?? "");
                }
            }
            Console.WriteLine($"{result["city"]?.ToString() ?? ""} {result["cityname"]?.ToString() ?? ""} {result["date"]?.ToString() ?? ""} {result["week"]?.ToString() ?? ""} {timestr} {result["numberrule"]?.ToString() ?? ""} {result["number"]?.ToString() ?? ""}");
            Console.WriteLine(result["area"]?.ToString() ?? "");
            Console.WriteLine(result["summary"]?.ToString() ?? "");
        }
        catch (HttpRequestException ex)
        {
            Console.WriteLine($"请求出错: {ex.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生错误: {ex.Message}");
        }
    }

    static async Task Main()
    {
        await VehicleLimit.CityAsync();
        await VehicleLimit.QueryAsync("hangzhou", "2025-4-22");
    }
}