BaoruhMile.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using Microsoft.Extensions.Logging;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace ConsoleApp1.Mile;
  9. internal class BaoruhMile
  10. {
  11. const string urlFormat = "https://e-bus.baoruh.com/carno/{0}";
  12. public static async Task<decimal?> GetMile(string carNumber, ILogger? logger = null)
  13. {
  14. try
  15. {
  16. HttpClient client = new HttpClient();
  17. string url = string.Format(urlFormat, carNumber);
  18. var result = await client.GetAsync(url);
  19. if (result.IsSuccessStatusCode)
  20. {
  21. //Stream contentStream = await result.Content.ReadAsStreamAsync();
  22. //var body = await new StreamReader(contentStream).ReadToEndAsync();
  23. var body = await result.Content.ReadAsStringAsync();
  24. JObject jo = JObject.Parse(body);
  25. string checkKey = jo.Value<string>("CarNumber");// jo["State"].Value<int>();
  26. if (checkKey == carNumber &&
  27. jo.ContainsKey("DR_MILE"))
  28. {
  29. decimal mileage = jo.Value<decimal>("DR_MILE");
  30. return mileage;
  31. }
  32. return 1;
  33. }
  34. }
  35. catch (Exception e)
  36. {
  37. }
  38. return -1;
  39. }
  40. }