| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- 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<IHttpClientFactory>();
- _httpClientService = new HttpClientService(httpClientFactory);
- }
- /// <summary>
- /// 发送油品信息给云端
- /// </summary>
- /// <param name="requestJson"></param>
- /// <returns></returns>
- public async Task<HttpResponseMessage> 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")
- };
- }
- }
- /// <summary>
- /// 更新油品信息给云端
- /// </summary>
- /// <param name="requestJson"></param>
- /// <returns></returns>
- public async Task<HttpResponseMessage> 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")
- };
- }
- }
- /// <summary>
- /// 删除油品信息发送给云端
- /// </summary>
- /// <param name="requestJson"></param>
- /// <returns></returns>
- public async Task<HttpResponseMessage> 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")
- };
- }
- }
- /// <summary>
- /// 发送油罐信息给云端
- /// </summary>
- /// <param name="requestJson"></param>
- /// <returns></returns>
- public async Task<HttpResponseMessage> 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")
- };
- }
- }
- /// <summary>
- /// 更新油罐信息给云端
- /// </summary>
- /// <param name="requestJson"></param>
- /// <returns></returns>
- public async Task<HttpResponseMessage> 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")
- };
- }
- }
- /// <summary>
- /// 删除油罐信息发送给云端
- /// </summary>
- /// <param name="requestJson"></param>
- /// <returns></returns>
- public async Task<HttpResponseMessage> 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")
- };
- }
- }
- /// <summary>
- /// 发送油枪信息给云端
- /// </summary>
- /// <param name="requestJson"></param>
- /// <returns></returns>
- public async Task<HttpResponseMessage> 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")
- };
- }
- }
- /// <summary>
- /// 更新油枪信息给云端
- /// </summary>
- /// <param name="requestJson"></param>
- /// <returns></returns>
- public async Task<HttpResponseMessage> 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")
- };
- }
- }
- /// <summary>
- /// 删除油枪信息发送给云端
- /// </summary>
- /// <param name="requestJson"></param>
- /// <returns></returns>
- public async Task<HttpResponseMessage> 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")
- };
- }
- }
- /// <summary>
- /// 更新油枪状态
- /// </summary>
- /// <param name="requestJson"></param>
- /// <returns></returns>
- public async Task<HttpResponseMessage> 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<HttpResponseMessage> 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")
- };
- }
- }
-
- }
- }
|