FuntoroMile.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using ConsoleApp1.Mile;
  2. using Newtonsoft.Json.Linq;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Net.Http.Headers;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace ConsoleApp1.Mile
  12. {
  13. internal class FuntoroMile
  14. {
  15. public static void Test()
  16. {
  17. var license = "EAA-127";
  18. var apiUrl = "http://cloud.funtoro.com/twchannel1/api/index.php/ebus";
  19. var apiKey = "FUN2yOf05bUS";
  20. new FuntoroMile().GetVehicleMileage(license, apiUrl, apiKey);
  21. }
  22. public double GetVehicleMileage(string VehicleLicense, string apiUrl, string apiKey)
  23. {
  24. double mileage = 0;
  25. var dt = DateTime.Now;
  26. var dto = new { Code = 106, VehicleLicense };
  27. var sJ = JsonConvert.SerializeObject(dto);
  28. var sign = GetSignature(apiKey, dt, sJ);
  29. var signedDto = new { Data = sJ, Ts = GetUnixTimeSeconds(dt), Signature = sign };
  30. var vvJ = JsonConvert.SerializeObject(signedDto);
  31. try
  32. {
  33. var client = new HttpClient() { Timeout = TimeSpan.FromMilliseconds(60000) };
  34. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  35. var response = client.PostAsync(apiUrl, new StringContent(vvJ, Encoding.UTF8, "application/json")).Result;
  36. string responseBody = response.Content.ReadAsStringAsync().Result;
  37. Console.WriteLine(responseBody);
  38. if (response.IsSuccessStatusCode)
  39. {
  40. JObject jo = JObject.Parse(responseBody);
  41. int state = jo["State"].Value<int>();
  42. if (state == 1)
  43. {
  44. mileage = jo["Mileage"].Value<double>();
  45. }
  46. }
  47. }
  48. catch (Exception e)
  49. {
  50. Console.WriteLine(string.Format(" {0} 呼叫 里程數API 發生問題 {1}", VehicleLicense, e.ToString()));
  51. }
  52. return mileage;
  53. }
  54. private string GetSignature(string apikey, DateTime dt, string requestBody)
  55. {
  56. long ts = 0;
  57. ts = GetUnixTimeSeconds(dt);
  58. //取得簽章
  59. var str = string.Format("{0}{1}{2}"
  60. , ts
  61. , requestBody
  62. , apikey);
  63. return BackendCrytography.MD5ToHex_UTF8(str);
  64. }
  65. private static long GetUnixTimeSeconds(DateTime dt)
  66. {
  67. DateTimeOffset _offset = new DateTimeOffset(dt);
  68. return DateTimeToUnixTimestamp(_offset.DateTime);
  69. }
  70. private static long DateTimeToUnixTimestamp(DateTime dateTime)
  71. {
  72. var start = new DateTime(1970, 1, 1, 0, 0, 0, dateTime.Kind);
  73. return Convert.ToInt64((dateTime - start).TotalSeconds);
  74. }
  75. }
  76. public class BackendCrytography
  77. {
  78. /// <summary>
  79. /// Returns a MD5 hash as a string
  80. /// </summary>
  81. /// <param name="TextToHash">String to be hashed.</param>
  82. /// <returns>Hash as string.</returns>
  83. public static String MD5ToHex_UTF8(String TextToHash)
  84. {
  85. if ((TextToHash == null) || (TextToHash.Length == 0))
  86. {
  87. return String.Empty;
  88. }
  89. MD5 md5 = new MD5CryptoServiceProvider();
  90. byte[] textToHash = Encoding.UTF8.GetBytes(TextToHash);
  91. byte[] result = md5.ComputeHash(textToHash);
  92. return BitConverter.ToString(result).Replace("-", "").ToLower();
  93. }
  94. /// <summary>
  95. /// Returns a MD5 hash as a string
  96. /// </summary>
  97. /// <param name="TextToHash">String to be hashed.</param>
  98. /// <returns>Hash as string.</returns>
  99. public static String GetMD5Hash(String TextToHash)
  100. {
  101. if ((TextToHash == null) || (TextToHash.Length == 0))
  102. {
  103. return String.Empty;
  104. }
  105. MD5 md5 = new MD5CryptoServiceProvider();
  106. //MD5 md5 = MD5.Create();
  107. //byte[] textToHash = Encoding.Default.GetBytes(TextToHash);
  108. byte[] textToHash = Encoding.UTF8.GetBytes(TextToHash);
  109. byte[] result = md5.ComputeHash(textToHash);
  110. var b64 = Convert.ToBase64String(result);
  111. return b64;
  112. }
  113. public static String GetSHA256Hash(String TextToHash)
  114. {
  115. if ((TextToHash == null) || (TextToHash.Length == 0))
  116. {
  117. return String.Empty;
  118. }
  119. SHA256 sha256 = new SHA256CryptoServiceProvider();//建立一個SHA256
  120. byte[] source = Encoding.Default.GetBytes(TextToHash);//將字串轉為Byte[]
  121. byte[] crypto = sha256.ComputeHash(source);//進行SHA256加密
  122. return Convert.ToBase64String(crypto);
  123. }
  124. public static string GetFileMD5Hash(string filePath)
  125. {
  126. using (var md5 = MD5.Create())
  127. {
  128. using (var stream = File.OpenRead(filePath))
  129. {
  130. return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
  131. }
  132. }
  133. }
  134. public static string GetFileMD5Hash(Stream stream)
  135. {
  136. using (var md5 = MD5.Create())
  137. {
  138. return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToUpper();
  139. }
  140. }
  141. }
  142. }