OuterHttpClient.cs 6.0 KB

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