HttpApiClient.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace XYY.Core.Standard.ApiClient
  9. {
  10. public static class HttpApiClientHelper
  11. {
  12. private static Dictionary<string, string> TestHost
  13. = new Dictionary<string, string>()
  14. {
  15. {"WMSHost","http://120.24.149.148/" }
  16. };
  17. private static Dictionary<string, string> ProductHost = new Dictionary<string, string>()
  18. {
  19. {"WMSHost","http://120.24.149.148/" }
  20. };
  21. #if Debug
  22. public static bool IsTest => true;
  23. #else
  24. public static bool IsTest => false;
  25. #endif
  26. public static string WMSHost => IsTest ? TestHost["WMSHost"] : ProductHost["WMSHost"];
  27. public static string AdminToken => "132A7468DE079C6CEB59F383A661E612";
  28. }
  29. public class RequestException : Exception
  30. {
  31. public RequestException(string message) : base(message)
  32. {
  33. }
  34. /// <summary>
  35. /// 原始返回值
  36. /// </summary>
  37. public string OriginalContent
  38. {
  39. get; set;
  40. }
  41. /// <summary>
  42. /// 错误信息
  43. /// </summary>
  44. public string ErrorMessage
  45. {
  46. get; set;
  47. }
  48. /// <summary>
  49. /// 请求Id
  50. /// </summary>
  51. public string RequestId
  52. {
  53. get; set;
  54. }
  55. public string RequestUrl
  56. {
  57. get; set;
  58. }
  59. public object Request
  60. {
  61. get; set;
  62. }
  63. }
  64. public class HttpApiClient
  65. {
  66. private string host;
  67. private string agentToken;
  68. private ILogger<HttpApiClient> logger;
  69. public HttpApiClient(string host, ILogger<HttpApiClient> logger)
  70. {
  71. this.host = host;
  72. this.logger = logger;
  73. }
  74. public HttpApiClient(string host, string agentToken, ILogger<HttpApiClient> logger)
  75. {
  76. this.host = host;
  77. this.agentToken = agentToken;
  78. this.logger = logger;
  79. }
  80. public string BaseHost { get => host; }
  81. public string AgentToken { get => agentToken; }
  82. public async Task<OutModel> Post<OutModel>(string action)
  83. {
  84. return await Post<OutModel, object>(action, null);
  85. }
  86. public async Task<OutModel> Post<OutModel, InMode>(string action, InMode inMode)
  87. {
  88. if (host.EndsWith("/") && action.StartsWith("/"))
  89. host = host.Remove(host.Length - 1);
  90. string url = host + action;
  91. string body = "";
  92. System.Net.WebClient client = new System.Net.WebClient();
  93. try
  94. {
  95. string token = agentToken;
  96. client.Headers.Add("token", token);
  97. client.Headers.Add("Authorization", "token " + token);
  98. client.Headers.Add("Content-Type", "application/json");
  99. client.Headers.Add("Accept", "application/json");
  100. if (inMode != null)
  101. body = Newtonsoft.Json.JsonConvert.SerializeObject(inMode);
  102. string data = string.Empty;
  103. if (inMode != null)
  104. data = await client.UploadStringTaskAsync(url, body);
  105. else
  106. data = await client.DownloadStringTaskAsync(url);
  107. try
  108. {
  109. OutModel model = Newtonsoft.Json.JsonConvert.DeserializeObject<OutModel>(data);
  110. return model;
  111. }
  112. catch (Exception ex)
  113. {
  114. throw new Exception(" Json 反序列化失败" + ex.Message);
  115. }
  116. }
  117. catch (WebException ex)
  118. {
  119. string requestId = Guid.NewGuid().ToString();
  120. string content = new System.IO.StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
  121. string message = ex.Status.ToString() + " " + ex.Message + " " + content;
  122. logger.LogError("http 请求失败{message} 请求url{url} body{body} request{requestId}", message, url, body, requestId);
  123. throw new Exception("http 请求失败" + message);
  124. }
  125. catch (Exception ex)
  126. {
  127. logger.LogError(ex.Message);
  128. throw new Exception("api 请求失败");
  129. }
  130. }
  131. }
  132. }