123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- using ConsoleApp1.Mile;
- using Newtonsoft.Json.Linq;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http.Headers;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1.Mile
- {
- internal class FuntoroMile
- {
- public static void Test()
- {
- var license = "EAA-127";
- var apiUrl = "http://cloud.funtoro.com/twchannel1/api/index.php/ebus";
- var apiKey = "FUN2yOf05bUS";
- new FuntoroMile().GetVehicleMileage(license, apiUrl, apiKey);
- }
- public double GetVehicleMileage(string VehicleLicense, string apiUrl, string apiKey)
- {
- double mileage = 0;
- var dt = DateTime.Now;
- var dto = new { Code = 106, VehicleLicense };
- var sJ = JsonConvert.SerializeObject(dto);
- var sign = GetSignature(apiKey, dt, sJ);
- var signedDto = new { Data = sJ, Ts = GetUnixTimeSeconds(dt), Signature = sign };
- var vvJ = JsonConvert.SerializeObject(signedDto);
- try
- {
- var client = new HttpClient() { Timeout = TimeSpan.FromMilliseconds(60000) };
- client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- var response = client.PostAsync(apiUrl, new StringContent(vvJ, Encoding.UTF8, "application/json")).Result;
- string responseBody = response.Content.ReadAsStringAsync().Result;
- Console.WriteLine(responseBody);
- if (response.IsSuccessStatusCode)
- {
- JObject jo = JObject.Parse(responseBody);
- int state = jo["State"].Value<int>();
- if (state == 1)
- {
- mileage = jo["Mileage"].Value<double>();
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(string.Format(" {0} 呼叫 里程數API 發生問題 {1}", VehicleLicense, e.ToString()));
- }
- return mileage;
- }
- private string GetSignature(string apikey, DateTime dt, string requestBody)
- {
- long ts = 0;
- ts = GetUnixTimeSeconds(dt);
- //取得簽章
- var str = string.Format("{0}{1}{2}"
- , ts
- , requestBody
- , apikey);
- return BackendCrytography.MD5ToHex_UTF8(str);
- }
- private static long GetUnixTimeSeconds(DateTime dt)
- {
- DateTimeOffset _offset = new DateTimeOffset(dt);
- return DateTimeToUnixTimestamp(_offset.DateTime);
- }
- private static long DateTimeToUnixTimestamp(DateTime dateTime)
- {
- var start = new DateTime(1970, 1, 1, 0, 0, 0, dateTime.Kind);
- return Convert.ToInt64((dateTime - start).TotalSeconds);
- }
- }
- public class BackendCrytography
- {
- /// <summary>
- /// Returns a MD5 hash as a string
- /// </summary>
- /// <param name="TextToHash">String to be hashed.</param>
- /// <returns>Hash as string.</returns>
- public static String MD5ToHex_UTF8(String TextToHash)
- {
- if ((TextToHash == null) || (TextToHash.Length == 0))
- {
- return String.Empty;
- }
- MD5 md5 = new MD5CryptoServiceProvider();
- byte[] textToHash = Encoding.UTF8.GetBytes(TextToHash);
- byte[] result = md5.ComputeHash(textToHash);
- return BitConverter.ToString(result).Replace("-", "").ToLower();
- }
- /// <summary>
- /// Returns a MD5 hash as a string
- /// </summary>
- /// <param name="TextToHash">String to be hashed.</param>
- /// <returns>Hash as string.</returns>
- public static String GetMD5Hash(String TextToHash)
- {
- if ((TextToHash == null) || (TextToHash.Length == 0))
- {
- return String.Empty;
- }
- MD5 md5 = new MD5CryptoServiceProvider();
- //MD5 md5 = MD5.Create();
- //byte[] textToHash = Encoding.Default.GetBytes(TextToHash);
- byte[] textToHash = Encoding.UTF8.GetBytes(TextToHash);
- byte[] result = md5.ComputeHash(textToHash);
- var b64 = Convert.ToBase64String(result);
- return b64;
- }
- public static String GetSHA256Hash(String TextToHash)
- {
- if ((TextToHash == null) || (TextToHash.Length == 0))
- {
- return String.Empty;
- }
- SHA256 sha256 = new SHA256CryptoServiceProvider();//建立一個SHA256
- byte[] source = Encoding.Default.GetBytes(TextToHash);//將字串轉為Byte[]
- byte[] crypto = sha256.ComputeHash(source);//進行SHA256加密
- return Convert.ToBase64String(crypto);
- }
- public static string GetFileMD5Hash(string filePath)
- {
- using (var md5 = MD5.Create())
- {
- using (var stream = File.OpenRead(filePath))
- {
- return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
- }
- }
- }
- public static string GetFileMD5Hash(Stream stream)
- {
- using (var md5 = MD5.Create())
- {
- return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToUpper();
- }
- }
- }
- }
|