OuterHttpClient.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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> Get(string url, Dictionary<string, string> headers, string saltkey)
  33. {
  34. HttpResult result = new HttpResult() { Success = false };
  35. try
  36. {
  37. string body = PreAction(url, ref headers, null, saltkey);
  38. var _response = await httpClient.GetJsonAsync(url, headers);
  39. result.Response = _response.Response;
  40. result.Status = _response.StatusCode;
  41. result.Success = _response.IsSuccessStatusCode;
  42. result.Exception = _response.Exception;
  43. }
  44. catch (Exception ex)
  45. {
  46. result.Exception = ex;
  47. }
  48. return result;
  49. }
  50. async public Task<HttpResult> Delete(string url, Dictionary<string, string> headers, string saltkey)
  51. {
  52. HttpResult result = new HttpResult() { Success = false };
  53. try
  54. {
  55. string body = PreAction(url, ref headers, null, saltkey);
  56. var _response = await httpClient.DeleteJsonAsync(url, headers);
  57. result.Response = _response.Response;
  58. result.Status = _response.StatusCode;
  59. result.Success = _response.IsSuccessStatusCode;
  60. result.Exception = _response.Exception;
  61. }
  62. catch (Exception ex)
  63. {
  64. result.Exception = ex;
  65. }
  66. return result;
  67. }
  68. async public Task<HttpResult> Put(string url, Dictionary<string, string> headers, object requestBody, string saltkey)
  69. {
  70. HttpResult result = new HttpResult() { Success = false };
  71. try
  72. {
  73. string body = PreAction(url, ref headers, requestBody, saltkey);
  74. var _response = await httpClient.PutJsonAsync(url, body, headers);
  75. result.Response = _response.Response;
  76. result.Status = _response.StatusCode;
  77. result.Success = _response.IsSuccessStatusCode;
  78. result.Exception = _response.Exception;
  79. }
  80. catch (Exception ex)
  81. {
  82. result.Exception = ex;
  83. }
  84. return result;
  85. }
  86. private string PreAction(string url, ref Dictionary<string, string> headers, object requestBody, string saltkey)
  87. {
  88. var _body = requestBody == null ? "" : JsonConvert.SerializeObject(requestBody, GlobalConfig.JSONSERIALIZER_FORMAT);
  89. headers.Add("Timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString());
  90. string signature = GetSignature(GetUnencodeText(url, _body, headers["Timestamp"], headers["PartnerId"], saltkey));
  91. headers.Add("Signature", signature);
  92. return _body;
  93. }
  94. private string GetUnencodeText(string url, string body, string timestamp, string partnerId, string saltkey)
  95. {
  96. string tempText = url.Substring(url.IndexOf('?') + 1).ToLower();
  97. tempText = tempText.StartsWith("http") ? string.Empty : tempText;
  98. body = tempText + body;
  99. string unencodeText = string.Format("{0}{1}{2}{3}", body, timestamp, partnerId, saltkey).ToLower();
  100. return unencodeText;
  101. }
  102. private string GetSignature(string unencodeText)
  103. {
  104. if ((unencodeText == null) || (unencodeText.Length == 0))
  105. {
  106. return String.Empty;
  107. }
  108. unencodeText = unencodeText.ToLower();
  109. MD5 md5 = new MD5CryptoServiceProvider();
  110. byte[] textToHash = Encoding.UTF8.GetBytes(unencodeText);
  111. byte[] result = md5.ComputeHash(textToHash);
  112. return BitConverter.ToString(result).Replace("-", "").ToLower();
  113. }
  114. }
  115. public class HttpResult
  116. {
  117. public int StatusCode { set; get; }
  118. public bool Success { set; get; }
  119. public HttpStatusCode Status { set; get; }
  120. public Exception Exception { set; get; }
  121. public string Response { set; get; }
  122. }
  123. }