1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- using Microsoft.Extensions.Logging;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1.Mile;
- internal class BaoruhMile
- {
- const string urlFormat = "https://e-bus.baoruh.com/carno/{0}";
- public static async Task<decimal?> GetMile(string carNumber, ILogger? logger = null)
- {
- try
- {
- HttpClient client = new HttpClient();
- string url = string.Format(urlFormat, carNumber);
- var result = await client.GetAsync(url);
- if (result.IsSuccessStatusCode)
- {
- //Stream contentStream = await result.Content.ReadAsStreamAsync();
- //var body = await new StreamReader(contentStream).ReadToEndAsync();
- var body = await result.Content.ReadAsStringAsync();
- JObject jo = JObject.Parse(body);
- string checkKey = jo.Value<string>("CarNumber");// jo["State"].Value<int>();
- if (checkKey == carNumber &&
- jo.ContainsKey("DR_MILE"))
- {
- decimal mileage = jo.Value<decimal>("DR_MILE");
- return mileage;
- }
- return 1;
- }
- }
- catch (Exception e)
- {
- }
- return -1;
- }
- }
|