using Edge.Core.HttpClient; using HengshanPaymentTerminal.Http.Request; using Microsoft.Extensions.DependencyInjection; using Org.BouncyCastle.Asn1.Ocsp; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; namespace HengshanPaymentTerminal.Http { public class HttpClientUtils:IHttpClientUtil { static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private IHttpClient _httpClientService; [ActivatorUtilitiesConstructor] public HttpClientUtils(IHttpClient httpClientService) { _httpClientService = httpClientService; } public HttpClientUtils() { ServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddHttpClient(); ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider(); IHttpClientFactory httpClientFactory = serviceProvider.GetService(); _httpClientService = new HttpClientService(httpClientFactory); } /// /// 发送油品信息给云端 /// /// /// public async Task SendOilInfo(string requestJson) { try { var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json"); return await _httpClientService.PostAsync("api/nozzle/uploadProduct", requesStr); } catch (Exception ex) { logger.Error(ex.Message); // 创建自定义错误对象 var error = new { Message = "发生错误", Error = ex.Message, StackTrace = ex.StackTrace }; // 返回 500 Internal Server Error 和 JSON 内容 return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json") }; } } /// /// 更新油品信息给云端 /// /// /// public async Task UpdateOilInfo(string requestJson) { try { var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json"); return await _httpClientService.PutAsync("api/nozzle/UpdateProduct", requesStr); } catch (Exception ex) { logger.Error(ex.Message); // 创建自定义错误对象 var error = new { Message = "发生错误", Error = ex.Message, StackTrace = ex.StackTrace }; // 返回 500 Internal Server Error 和 JSON 内容 return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json") }; } } /// /// 删除油品信息发送给云端 /// /// /// public async Task DeleteOilInfo(string requestJson) { try { var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json"); return await _httpClientService.DeleteAsync("api/nozzle/DeleteProduct", requesStr); } catch (Exception ex) { logger.Error(ex.Message); // 创建自定义错误对象 var error = new { Message = "发生错误", Error = ex.Message, StackTrace = ex.StackTrace }; // 返回 500 Internal Server Error 和 JSON 内容 return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json") }; } } /// /// 发送油罐信息给云端 /// /// /// public async Task SendTankInfo(string requestJson) { try { var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json"); return await _httpClientService.PostAsync("api/nozzle/uploadTanks", requesStr); } catch (Exception ex) { logger.Error(ex.Message); // 创建自定义错误对象 var error = new { Message = "发生错误", Error = ex.Message, StackTrace = ex.StackTrace }; // 返回 500 Internal Server Error 和 JSON 内容 return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json") }; } } /// /// 更新油罐信息给云端 /// /// /// public async Task UpdateTankInfo(string requestJson) { try { var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json"); return await _httpClientService.PutAsync("api/nozzle/UpdateTanks", requesStr); ; } catch (Exception ex) { logger.Error(ex.Message); // 创建自定义错误对象 var error = new { Message = "发生错误", Error = ex.Message, StackTrace = ex.StackTrace }; // 返回 500 Internal Server Error 和 JSON 内容 return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json") }; } } /// /// 删除油罐信息发送给云端 /// /// /// public async Task DeleteTankInfo(string requestJson) { try { var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json"); return await _httpClientService.DeleteAsync("api/nozzle/DeleteTanks", requesStr); } catch (Exception ex) { logger.Error(ex.Message); // 创建自定义错误对象 var error = new { Message = "发生错误", Error = ex.Message, StackTrace = ex.StackTrace }; // 返回 500 Internal Server Error 和 JSON 内容 return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json") }; } } /// /// 发送油枪信息给云端 /// /// /// public async Task SendNozzleInfo(string requestJson) { try { var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json"); return await _httpClientService.PostAsync("api/nozzle/uploadNozzle", requesStr); } catch (Exception ex) { logger.Error(ex.Message); // 创建自定义错误对象 var error = new { Message = "发生错误", Error = ex.Message, StackTrace = ex.StackTrace }; // 返回 500 Internal Server Error 和 JSON 内容 return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json") }; } } /// /// 更新油枪信息给云端 /// /// /// public async Task UpdateNozzleInfo(string requestJson) { try { var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json"); return await _httpClientService.PutAsync("api/nozzle/UpdateNozzle ", requesStr); } catch (Exception ex) { logger.Error(ex.Message); // 创建自定义错误对象 var error = new { Message = "发生错误", Error = ex.Message, StackTrace = ex.StackTrace }; // 返回 500 Internal Server Error 和 JSON 内容 return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json") }; } } /// /// 删除油枪信息发送给云端 /// /// /// public async Task DeleteNozzleInfo(string requestJson) { try { var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json"); return await _httpClientService.DeleteAsync("api/nozzle/DeleteNozzle", requesStr); } catch (Exception ex) { logger.Error(ex.Message); // 创建自定义错误对象 var error = new { Message = "发生错误", Error = ex.Message, StackTrace = ex.StackTrace }; // 返回 500 Internal Server Error 和 JSON 内容 return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json") }; } } /// /// 更新油枪状态 /// /// /// public async Task SendNozzleStatu(string requestJson) { try { var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json"); return await _httpClientService.PutAsync("api/nozzle/updateNozzleStatus", requesStr); } catch (Exception ex) { logger.Error(ex.Message); // 创建自定义错误对象 var error = new { Message = "发生错误", Error = ex.Message, StackTrace = ex.StackTrace }; // 返回 500 Internal Server Error 和 JSON 内容 return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json") }; } } public async Task CreateTransaction(string requestJson) { try { var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json"); return await _httpClientService.PostAsync("api/Transactions/CreateTransactions", requesStr); } catch (Exception ex) { logger.Error(ex.Message); // 创建自定义错误对象 var error = new { Message = "发生错误", Error = ex.Message, StackTrace = ex.StackTrace }; // 返回 500 Internal Server Error 和 JSON 内容 return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json") }; } } } }