OuterHttpClient.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using EVCB_OCPP.TaskScheduler.Services;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace EVCB_OCPP.TaskScheduler
  12. {
  13. public class OuterHttpClient
  14. {
  15. private HttpClientService httpClient = new HttpClientService();
  16. async public Task<HttpResult> Post(string url, Dictionary<string, string> headers, string requestBody, string saltkey)
  17. {
  18. HttpResult result = new HttpResult() { Success = false };
  19. try
  20. {
  21. string body = PreAction(url, ref headers, requestBody, saltkey);
  22. var _response = await httpClient.PostJsonAsync(url, body, headers);
  23. result.Response = _response.Response;
  24. result.Status = _response.StatusCode;
  25. result.Success = _response.IsSuccessStatusCode;
  26. result.Exception = _response.Exception;
  27. }
  28. catch (Exception ex)
  29. {
  30. result.Exception = ex;
  31. }
  32. return result;
  33. }
  34. async public Task<HttpResult> Get(string url, Dictionary<string, string> headers, string saltkey)
  35. {
  36. HttpResult result = new HttpResult() { Success = false };
  37. try
  38. {
  39. string body = PreAction(url, ref headers, null, saltkey);
  40. var _response = await httpClient.GetJsonAsync(url, headers);
  41. result.Response = _response.Response;
  42. result.Status = _response.StatusCode;
  43. result.Success = _response.IsSuccessStatusCode;
  44. result.Exception = _response.Exception;
  45. }
  46. catch (Exception ex)
  47. {
  48. result.Exception = ex;
  49. }
  50. return result;
  51. }
  52. async public Task<HttpResult> Delete(string url, Dictionary<string, string> headers, string saltkey)
  53. {
  54. HttpResult result = new HttpResult() { Success = false };
  55. try
  56. {
  57. string body = PreAction(url, ref headers, null, saltkey);
  58. var _response = await httpClient.DeleteJsonAsync(url, headers);
  59. result.Response = _response.Response;
  60. result.Status = _response.StatusCode;
  61. result.Success = _response.IsSuccessStatusCode;
  62. result.Exception = _response.Exception;
  63. }
  64. catch (Exception ex)
  65. {
  66. result.Exception = ex;
  67. }
  68. return result;
  69. }
  70. async public Task<HttpResult> Put(string url, Dictionary<string, string> headers, string requestBody, string saltkey)
  71. {
  72. HttpResult result = new HttpResult() { Success = false };
  73. try
  74. {
  75. string body = PreAction(url, ref headers, requestBody, saltkey);
  76. var _response = await httpClient.PutJsonAsync(url, body, headers);
  77. result.Response = _response.Response;
  78. result.Status = _response.StatusCode;
  79. result.Success = _response.IsSuccessStatusCode;
  80. result.Exception = _response.Exception;
  81. }
  82. catch (Exception ex)
  83. {
  84. result.Exception = ex;
  85. }
  86. return result;
  87. }
  88. private string PreAction(string url, ref Dictionary<string, string> headers, string requestBody, string saltkey)
  89. {
  90. var _body = requestBody == null ? "" : requestBody;
  91. headers.Add("Timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString());
  92. string signature = GetSignature(GetUnencodeText(url, _body, headers["Timestamp"], headers["PartnerId"], saltkey));
  93. headers.Add("Signature", signature);
  94. return _body;
  95. }
  96. private string GetUnencodeText(string url, string body, string timestamp, string partnerId, string saltkey)
  97. {
  98. string tempText = url.Substring(url.IndexOf('?') + 1).ToLower();
  99. tempText = tempText.StartsWith("http") ? string.Empty : tempText;
  100. body = tempText + body;
  101. string unencodeText = string.Format("{0}{1}{2}{3}", body, timestamp, partnerId, saltkey).ToLower();
  102. return unencodeText;
  103. }
  104. private string GetSignature(string unencodeText)
  105. {
  106. if ((unencodeText == null) || (unencodeText.Length == 0))
  107. {
  108. return String.Empty;
  109. }
  110. unencodeText = unencodeText.ToLower();
  111. MD5 md5 = new MD5CryptoServiceProvider();
  112. byte[] textToHash = Encoding.UTF8.GetBytes(unencodeText);
  113. byte[] result = md5.ComputeHash(textToHash);
  114. return BitConverter.ToString(result).Replace("-", "").ToLower();
  115. }
  116. }
  117. public class HttpResult
  118. {
  119. public int StatusCode { set; get; }
  120. public bool Success { set; get; }
  121. public HttpStatusCode Status { set; get; }
  122. public Exception Exception { set; get; }
  123. public string Response { set; get; }
  124. }
  125. }