OuterHttpClient.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 = new HttpClientService();
  13. async public Task<HttpResult> Post(string url, Dictionary<string, string> headers, object requestBody, string saltkey)
  14. {
  15. HttpResult result = new HttpResult() { Success = false };
  16. try
  17. {
  18. string body = PreAction(url, ref headers, requestBody, saltkey);
  19. var _response = await httpClient.PostJsonAsync(url, body, headers);
  20. result.Response = _response.Response;
  21. result.Status = _response.StatusCode;
  22. result.Success = _response.IsSuccessStatusCode;
  23. result.Exception = _response.Exception;
  24. }
  25. catch (Exception ex)
  26. {
  27. result.Exception = ex;
  28. }
  29. return result;
  30. }
  31. async public Task<HttpResult> PostFormDataAsync(string url, Dictionary<string, string> bodyData, Dictionary<string, string> headers, string clientName = "Default", bool bearerToken = false, string authorizationToken = null)
  32. {
  33. HttpResult result = new HttpResult() { Success = false };
  34. try
  35. {
  36. var _response = await httpClient.PostFormDataAsync(url, bodyData, null);
  37. result.Response = _response.Response;
  38. result.Status = _response.StatusCode;
  39. result.Success = _response.IsSuccessStatusCode;
  40. result.Exception = _response.Exception;
  41. }
  42. catch (Exception ex)
  43. {
  44. result.Exception = ex;
  45. }
  46. return result;
  47. }
  48. async public Task<HttpResult> GetWeather(string url, Dictionary<string, string> headers, string saltkey)
  49. {
  50. HttpResult result = new HttpResult() { Success = false };
  51. try
  52. {
  53. // string body = PreAction(url, ref headers, null, saltkey);
  54. var _response = await httpClient.GetJsonAsync(url, headers);
  55. result.Response = _response.Response;
  56. result.Status = _response.StatusCode;
  57. result.Success = _response.IsSuccessStatusCode;
  58. result.Exception = _response.Exception;
  59. }
  60. catch (Exception ex)
  61. {
  62. result.Exception = ex;
  63. }
  64. return result;
  65. }
  66. async public Task<HttpResult> Get(string url, Dictionary<string, string> headers, string saltkey)
  67. {
  68. HttpResult result = new HttpResult() { Success = false };
  69. try
  70. {
  71. string body = PreAction(url, ref headers, null, saltkey);
  72. var _response = await httpClient.GetJsonAsync(url, headers);
  73. result.Response = _response.Response;
  74. result.Status = _response.StatusCode;
  75. result.Success = _response.IsSuccessStatusCode;
  76. result.Exception = _response.Exception;
  77. }
  78. catch (Exception ex)
  79. {
  80. result.Exception = ex;
  81. }
  82. return result;
  83. }
  84. async public Task<HttpResult> Delete(string url, Dictionary<string, string> headers, string saltkey)
  85. {
  86. HttpResult result = new HttpResult() { Success = false };
  87. try
  88. {
  89. string body = PreAction(url, ref headers, null, saltkey);
  90. var _response = await httpClient.DeleteJsonAsync(url, headers);
  91. result.Response = _response.Response;
  92. result.Status = _response.StatusCode;
  93. result.Success = _response.IsSuccessStatusCode;
  94. result.Exception = _response.Exception;
  95. }
  96. catch (Exception ex)
  97. {
  98. result.Exception = ex;
  99. }
  100. return result;
  101. }
  102. async public Task<HttpResult> Put(string url, Dictionary<string, string> headers, object requestBody, string saltkey)
  103. {
  104. HttpResult result = new HttpResult() { Success = false };
  105. try
  106. {
  107. string body = PreAction(url, ref headers, requestBody, saltkey);
  108. var _response = await httpClient.PutJsonAsync(url, body, headers);
  109. result.Response = _response.Response;
  110. result.Status = _response.StatusCode;
  111. result.Success = _response.IsSuccessStatusCode;
  112. result.Exception = _response.Exception;
  113. }
  114. catch (Exception ex)
  115. {
  116. result.Exception = ex;
  117. }
  118. return result;
  119. }
  120. private string PreAction(string url, ref Dictionary<string, string> headers, object requestBody, string saltkey)
  121. {
  122. var _body = requestBody == null ? "" : JsonConvert.SerializeObject(requestBody, GlobalConfig.JSONSERIALIZER_FORMAT);
  123. headers.Add("Timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString());
  124. string signature = GetSignature(GetUnencodeText(url, _body, headers["Timestamp"], headers["PartnerId"], saltkey));
  125. headers.Add("Signature", signature);
  126. return _body;
  127. }
  128. private string GetUnencodeText(string url, string body, string timestamp, string partnerId, string saltkey)
  129. {
  130. string tempText = url.Substring(url.IndexOf('?') + 1).ToLower();
  131. tempText = tempText.StartsWith("http") ? string.Empty : tempText;
  132. body = tempText + body;
  133. string unencodeText = string.Format("{0}{1}{2}{3}", body, timestamp, partnerId, saltkey).ToLower();
  134. return unencodeText;
  135. }
  136. private string GetSignature(string unencodeText)
  137. {
  138. if ((unencodeText == null) || (unencodeText.Length == 0))
  139. {
  140. return String.Empty;
  141. }
  142. unencodeText = unencodeText.ToLower();
  143. MD5 md5 = new MD5CryptoServiceProvider();
  144. byte[] textToHash = Encoding.UTF8.GetBytes(unencodeText);
  145. byte[] result = md5.ComputeHash(textToHash);
  146. return BitConverter.ToString(result).Replace("-", "").ToLower();
  147. }
  148. }
  149. public class HttpResult
  150. {
  151. public int StatusCode { set; get; }
  152. public bool Success { set; get; }
  153. public HttpStatusCode Status { set; get; }
  154. public Exception Exception { set; get; }
  155. public string Response { set; get; }
  156. }
  157. }