OuterHttpClient.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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> Get(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> Delete(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.DeleteJsonAsync(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> Put(string url, Dictionary<string, string> headers, object requestBody, string saltkey)
  85. {
  86. HttpResult result = new HttpResult() { Success = false };
  87. try
  88. {
  89. string body = PreAction(url, ref headers, requestBody, saltkey);
  90. var _response = await httpClient.PutJsonAsync(url, body, 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. private string PreAction(string url, ref Dictionary<string, string> headers, object requestBody, string saltkey)
  103. {
  104. var _body = requestBody == null ? "" : JsonConvert.SerializeObject(requestBody, GlobalConfig.JSONSERIALIZER_FORMAT);
  105. headers.Add("Timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString());
  106. string signature = GetSignature(GetUnencodeText(url, _body, headers["Timestamp"], headers["PartnerId"], saltkey));
  107. headers.Add("Signature", signature);
  108. return _body;
  109. }
  110. private string GetUnencodeText(string url, string body, string timestamp, string partnerId, string saltkey)
  111. {
  112. string tempText = url.Substring(url.IndexOf('?') + 1).ToLower();
  113. tempText = tempText.StartsWith("http") ? string.Empty : tempText;
  114. body = tempText + body;
  115. string unencodeText = string.Format("{0}{1}{2}{3}", body, timestamp, partnerId, saltkey).ToLower();
  116. return unencodeText;
  117. }
  118. private string GetSignature(string unencodeText)
  119. {
  120. if ((unencodeText == null) || (unencodeText.Length == 0))
  121. {
  122. return String.Empty;
  123. }
  124. unencodeText = unencodeText.ToLower();
  125. MD5 md5 = new MD5CryptoServiceProvider();
  126. byte[] textToHash = Encoding.UTF8.GetBytes(unencodeText);
  127. byte[] result = md5.ComputeHash(textToHash);
  128. return BitConverter.ToString(result).Replace("-", "").ToLower();
  129. }
  130. }
  131. public class HttpResult
  132. {
  133. public int StatusCode { set; get; }
  134. public bool Success { set; get; }
  135. public HttpStatusCode Status { set; get; }
  136. public Exception Exception { set; get; }
  137. public string Response { set; get; }
  138. }
  139. }