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

获取城市示例代码C#

作者: 阅读数:1121 上传时间: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 CityAsync()
    {
        try
        {
            using var client = new HttpClient();
            string url = $"https://api.jisuapi.com/vehiclelimit/city?appkey={AppKey}";

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

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

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

            JArray resultArray = (JArray)jsonarr["result"];
            foreach (JObject val in resultArray)
            {
                Console.WriteLine($"{val["city"]} {val["cityname"]}
"); } } 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"); } }