HttpClientUtils.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. using Edge.Core.HttpClient;
  2. using HengshanPaymentTerminal.Http.Request;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using Org.BouncyCastle.Asn1.Ocsp;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Http;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Newtonsoft.Json;
  13. namespace HengshanPaymentTerminal.Http
  14. {
  15. public class HttpClientUtils:IHttpClientUtil
  16. {
  17. static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
  18. private IHttpClient _httpClientService;
  19. [ActivatorUtilitiesConstructor]
  20. public HttpClientUtils(IHttpClient httpClientService)
  21. {
  22. _httpClientService = httpClientService;
  23. }
  24. public HttpClientUtils()
  25. {
  26. ServiceCollection serviceCollection = new ServiceCollection();
  27. serviceCollection.AddHttpClient();
  28. ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
  29. IHttpClientFactory httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
  30. _httpClientService = new HttpClientService(httpClientFactory);
  31. }
  32. /// <summary>
  33. /// 发送油品信息给云端
  34. /// </summary>
  35. /// <param name="requestJson"></param>
  36. /// <returns></returns>
  37. public async Task<HttpResponseMessage> SendOilInfo(string requestJson)
  38. {
  39. try
  40. {
  41. var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
  42. return await _httpClientService.PostAsync("api/nozzle/uploadProduct", requesStr);
  43. }
  44. catch (Exception ex)
  45. {
  46. logger.Error(ex.Message);
  47. // 创建自定义错误对象
  48. var error = new
  49. {
  50. Message = "发生错误",
  51. Error = ex.Message,
  52. StackTrace = ex.StackTrace
  53. };
  54. // 返回 500 Internal Server Error 和 JSON 内容
  55. return new HttpResponseMessage(HttpStatusCode.InternalServerError)
  56. {
  57. Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json")
  58. };
  59. }
  60. }
  61. /// <summary>
  62. /// 更新油品信息给云端
  63. /// </summary>
  64. /// <param name="requestJson"></param>
  65. /// <returns></returns>
  66. public async Task<HttpResponseMessage> UpdateOilInfo(string requestJson)
  67. {
  68. try
  69. {
  70. var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
  71. return await _httpClientService.PutAsync("api/nozzle/UpdateProduct", requesStr);
  72. }
  73. catch (Exception ex)
  74. {
  75. logger.Error(ex.Message);
  76. // 创建自定义错误对象
  77. var error = new
  78. {
  79. Message = "发生错误",
  80. Error = ex.Message,
  81. StackTrace = ex.StackTrace
  82. };
  83. // 返回 500 Internal Server Error 和 JSON 内容
  84. return new HttpResponseMessage(HttpStatusCode.InternalServerError)
  85. {
  86. Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json")
  87. };
  88. }
  89. }
  90. /// <summary>
  91. /// 删除油品信息发送给云端
  92. /// </summary>
  93. /// <param name="requestJson"></param>
  94. /// <returns></returns>
  95. public async Task<HttpResponseMessage> DeleteOilInfo(string requestJson)
  96. {
  97. try
  98. {
  99. var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
  100. return await _httpClientService.DeleteAsync("api/nozzle/DeleteProduct", requesStr);
  101. }
  102. catch (Exception ex)
  103. {
  104. logger.Error(ex.Message);
  105. // 创建自定义错误对象
  106. var error = new
  107. {
  108. Message = "发生错误",
  109. Error = ex.Message,
  110. StackTrace = ex.StackTrace
  111. };
  112. // 返回 500 Internal Server Error 和 JSON 内容
  113. return new HttpResponseMessage(HttpStatusCode.InternalServerError)
  114. {
  115. Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json")
  116. };
  117. }
  118. }
  119. /// <summary>
  120. /// 发送油罐信息给云端
  121. /// </summary>
  122. /// <param name="requestJson"></param>
  123. /// <returns></returns>
  124. public async Task<HttpResponseMessage> SendTankInfo(string requestJson)
  125. {
  126. try
  127. {
  128. var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
  129. return await _httpClientService.PostAsync("api/nozzle/uploadTanks", requesStr);
  130. }
  131. catch (Exception ex)
  132. {
  133. logger.Error(ex.Message);
  134. // 创建自定义错误对象
  135. var error = new
  136. {
  137. Message = "发生错误",
  138. Error = ex.Message,
  139. StackTrace = ex.StackTrace
  140. };
  141. // 返回 500 Internal Server Error 和 JSON 内容
  142. return new HttpResponseMessage(HttpStatusCode.InternalServerError)
  143. {
  144. Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json")
  145. };
  146. }
  147. }
  148. /// <summary>
  149. /// 更新油罐信息给云端
  150. /// </summary>
  151. /// <param name="requestJson"></param>
  152. /// <returns></returns>
  153. public async Task<HttpResponseMessage> UpdateTankInfo(string requestJson)
  154. {
  155. try
  156. {
  157. var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
  158. return await _httpClientService.PutAsync("api/nozzle/UpdateTanks", requesStr); ;
  159. }
  160. catch (Exception ex)
  161. {
  162. logger.Error(ex.Message);
  163. // 创建自定义错误对象
  164. var error = new
  165. {
  166. Message = "发生错误",
  167. Error = ex.Message,
  168. StackTrace = ex.StackTrace
  169. };
  170. // 返回 500 Internal Server Error 和 JSON 内容
  171. return new HttpResponseMessage(HttpStatusCode.InternalServerError)
  172. {
  173. Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json")
  174. };
  175. }
  176. }
  177. /// <summary>
  178. /// 删除油罐信息发送给云端
  179. /// </summary>
  180. /// <param name="requestJson"></param>
  181. /// <returns></returns>
  182. public async Task<HttpResponseMessage> DeleteTankInfo(string requestJson)
  183. {
  184. try
  185. {
  186. var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
  187. return await _httpClientService.DeleteAsync("api/nozzle/DeleteTanks", requesStr);
  188. }
  189. catch (Exception ex)
  190. {
  191. logger.Error(ex.Message);
  192. // 创建自定义错误对象
  193. var error = new
  194. {
  195. Message = "发生错误",
  196. Error = ex.Message,
  197. StackTrace = ex.StackTrace
  198. };
  199. // 返回 500 Internal Server Error 和 JSON 内容
  200. return new HttpResponseMessage(HttpStatusCode.InternalServerError)
  201. {
  202. Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json")
  203. };
  204. }
  205. }
  206. /// <summary>
  207. /// 发送油枪信息给云端
  208. /// </summary>
  209. /// <param name="requestJson"></param>
  210. /// <returns></returns>
  211. public async Task<HttpResponseMessage> SendNozzleInfo(string requestJson)
  212. {
  213. try
  214. {
  215. var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
  216. return await _httpClientService.PostAsync("api/nozzle/uploadNozzle", requesStr);
  217. }
  218. catch (Exception ex)
  219. {
  220. logger.Error(ex.Message);
  221. // 创建自定义错误对象
  222. var error = new
  223. {
  224. Message = "发生错误",
  225. Error = ex.Message,
  226. StackTrace = ex.StackTrace
  227. };
  228. // 返回 500 Internal Server Error 和 JSON 内容
  229. return new HttpResponseMessage(HttpStatusCode.InternalServerError)
  230. {
  231. Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json")
  232. };
  233. }
  234. }
  235. /// <summary>
  236. /// 更新油枪信息给云端
  237. /// </summary>
  238. /// <param name="requestJson"></param>
  239. /// <returns></returns>
  240. public async Task<HttpResponseMessage> UpdateNozzleInfo(string requestJson)
  241. {
  242. try
  243. {
  244. var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
  245. return await _httpClientService.PutAsync("api/nozzle/UpdateNozzle ", requesStr);
  246. }
  247. catch (Exception ex)
  248. {
  249. logger.Error(ex.Message);
  250. // 创建自定义错误对象
  251. var error = new
  252. {
  253. Message = "发生错误",
  254. Error = ex.Message,
  255. StackTrace = ex.StackTrace
  256. };
  257. // 返回 500 Internal Server Error 和 JSON 内容
  258. return new HttpResponseMessage(HttpStatusCode.InternalServerError)
  259. {
  260. Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json")
  261. };
  262. }
  263. }
  264. /// <summary>
  265. /// 删除油枪信息发送给云端
  266. /// </summary>
  267. /// <param name="requestJson"></param>
  268. /// <returns></returns>
  269. public async Task<HttpResponseMessage> DeleteNozzleInfo(string requestJson)
  270. {
  271. try
  272. {
  273. var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
  274. return await _httpClientService.DeleteAsync("api/nozzle/DeleteNozzle", requesStr);
  275. }
  276. catch (Exception ex)
  277. {
  278. logger.Error(ex.Message);
  279. // 创建自定义错误对象
  280. var error = new
  281. {
  282. Message = "发生错误",
  283. Error = ex.Message,
  284. StackTrace = ex.StackTrace
  285. };
  286. // 返回 500 Internal Server Error 和 JSON 内容
  287. return new HttpResponseMessage(HttpStatusCode.InternalServerError)
  288. {
  289. Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json")
  290. };
  291. }
  292. }
  293. /// <summary>
  294. /// 更新油枪状态
  295. /// </summary>
  296. /// <param name="requestJson"></param>
  297. /// <returns></returns>
  298. public async Task<HttpResponseMessage> SendNozzleStatu(string requestJson)
  299. {
  300. try
  301. {
  302. var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
  303. return await _httpClientService.PutAsync("api/nozzle/updateNozzleStatus", requesStr);
  304. }
  305. catch (Exception ex)
  306. {
  307. logger.Error(ex.Message);
  308. // 创建自定义错误对象
  309. var error = new
  310. {
  311. Message = "发生错误",
  312. Error = ex.Message,
  313. StackTrace = ex.StackTrace
  314. };
  315. // 返回 500 Internal Server Error 和 JSON 内容
  316. return new HttpResponseMessage(HttpStatusCode.InternalServerError)
  317. {
  318. Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json")
  319. };
  320. }
  321. }
  322. public async Task<HttpResponseMessage> CreateTransaction(string requestJson)
  323. {
  324. try
  325. {
  326. var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
  327. return await _httpClientService.PostAsync("api/Transactions/CreateTransactions", requesStr);
  328. }
  329. catch (Exception ex)
  330. {
  331. logger.Error(ex.Message);
  332. // 创建自定义错误对象
  333. var error = new
  334. {
  335. Message = "发生错误",
  336. Error = ex.Message,
  337. StackTrace = ex.StackTrace
  338. };
  339. // 返回 500 Internal Server Error 和 JSON 内容
  340. return new HttpResponseMessage(HttpStatusCode.InternalServerError)
  341. {
  342. Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json")
  343. };
  344. }
  345. }
  346. }
  347. }