OuterHttpClient.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Net;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace EVCB_OCPP.WSServer.Service
  9. {
  10. public class OuterHttpClient
  11. {
  12. private HttpClientService httpClient;
  13. public OuterHttpClient(HttpClientService httpClient)
  14. {
  15. this.httpClient = httpClient;
  16. }
  17. async public Task<HttpResult> Post(string url, Dictionary<string, string> headers, object requestBody, string saltkey)
  18. {
  19. HttpResult result = new HttpResult() { Success = false };
  20. try
  21. {
  22. string body = PreAction(url, ref headers, requestBody, saltkey);
  23. var _response = await httpClient.PostJsonAsync(url, body, headers);
  24. result.Response = _response.Response;
  25. result.Status = _response.StatusCode;
  26. result.Success = _response.IsSuccessStatusCode;
  27. result.Exception = _response.Exception;
  28. }
  29. catch (Exception ex)
  30. {
  31. result.Exception = ex;
  32. }
  33. return result;
  34. }
  35. async public Task<HttpResult> PostFormDataAsync(string url, Dictionary<string, string> bodyData, Dictionary<string, string> headers, string clientName = "Default", bool bearerToken = false, string authorizationToken = null)
  36. {
  37. HttpResult result = new HttpResult() { Success = false };
  38. try
  39. {
  40. var _response = await httpClient.PostFormDataAsync(url, bodyData, null);
  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> GetWeather(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.GetJsonAsync(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> Get(string url, Dictionary<string, string> headers, string saltkey)
  71. {
  72. HttpResult result = new HttpResult() { Success = false };
  73. try
  74. {
  75. string body = PreAction(url, ref headers, null, saltkey);
  76. var _response = await httpClient.GetJsonAsync(url, 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. async public Task<HttpResult> Delete(string url, Dictionary<string, string> headers, string saltkey)
  89. {
  90. HttpResult result = new HttpResult() { Success = false };
  91. try
  92. {
  93. string body = PreAction(url, ref headers, null, saltkey);
  94. var _response = await httpClient.DeleteJsonAsync(url, headers);
  95. result.Response = _response.Response;
  96. result.Status = _response.StatusCode;
  97. result.Success = _response.IsSuccessStatusCode;
  98. result.Exception = _response.Exception;
  99. }
  100. catch (Exception ex)
  101. {
  102. result.Exception = ex;
  103. }
  104. return result;
  105. }
  106. async public Task<HttpResult> Put(string url, Dictionary<string, string> headers, object requestBody, string saltkey)
  107. {
  108. HttpResult result = new HttpResult() { Success = false };
  109. try
  110. {
  111. string body = PreAction(url, ref headers, requestBody, saltkey);
  112. var _response = await httpClient.PutJsonAsync(url, body, headers);
  113. result.Response = _response.Response;
  114. result.Status = _response.StatusCode;
  115. result.Success = _response.IsSuccessStatusCode;
  116. result.Exception = _response.Exception;
  117. }
  118. catch (Exception ex)
  119. {
  120. result.Exception = ex;
  121. }
  122. return result;
  123. }
  124. private string PreAction(string url, ref Dictionary<string, string> headers, object requestBody, string saltkey)
  125. {
  126. var _body = requestBody == null ? "" : JsonConvert.SerializeObject(requestBody, GlobalConfig.JSONSERIALIZER_FORMAT);
  127. headers.Add("Timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString());
  128. string signature = GetSignature(GetUnencodeText(url, _body, headers["Timestamp"], headers["PartnerId"], saltkey));
  129. headers.Add("Signature", signature);
  130. return _body;
  131. }
  132. private string GetUnencodeText(string url, string body, string timestamp, string partnerId, string saltkey)
  133. {
  134. string tempText = url.Substring(url.IndexOf('?') + 1).ToLower();
  135. tempText = tempText.StartsWith("http") ? string.Empty : tempText;
  136. body = tempText + body;
  137. string unencodeText = string.Format("{0}{1}{2}{3}", body, timestamp, partnerId, saltkey).ToLower();
  138. return unencodeText;
  139. }
  140. private string GetSignature(string unencodeText)
  141. {
  142. if ((unencodeText == null) || (unencodeText.Length == 0))
  143. {
  144. return String.Empty;
  145. }
  146. unencodeText = unencodeText.ToLower();
  147. MD5 md5 = MD5.Create();// new MD5CryptoServiceProvider();
  148. byte[] textToHash = Encoding.UTF8.GetBytes(unencodeText);
  149. byte[] result = md5.ComputeHash(textToHash);
  150. return BitConverter.ToString(result).Replace("-", "").ToLower();
  151. }
  152. }
  153. public class HttpResult
  154. {
  155. public int StatusCode { set; get; }
  156. public bool Success { set; get; }
  157. public HttpStatusCode Status { set; get; }
  158. public Exception Exception { set; get; }
  159. public string Response { set; get; }
  160. }
  161. }