HttpClientUtils.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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.Http;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace HengshanPaymentTerminal.Http
  12. {
  13. public class HttpClientUtils:IHttpClientUtil
  14. {
  15. private IHttpClient _httpClientService;
  16. [ActivatorUtilitiesConstructor]
  17. public HttpClientUtils(IHttpClient httpClientService)
  18. {
  19. _httpClientService = httpClientService;
  20. }
  21. public HttpClientUtils()
  22. {
  23. ServiceCollection serviceCollection = new ServiceCollection();
  24. serviceCollection.AddHttpClient();
  25. ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
  26. IHttpClientFactory httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
  27. _httpClientService = new HttpClientService(httpClientFactory);
  28. }
  29. public async Task<HttpResponseMessage> CreateTransaction(string requestJson)
  30. {
  31. var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
  32. return await _httpClientService.PostAsync("api/transactions/CreateTransaction", requesStr);
  33. }
  34. public async Task<HttpResponseMessage> SendNozzleInfo(string requestJson)
  35. {
  36. var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
  37. return await _httpClientService.PostAsync("api/nozzle/uploadNozzle", requesStr);
  38. }
  39. public async Task<HttpResponseMessage> SendNozzleStatu(string requestJson)
  40. {
  41. var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
  42. return await _httpClientService.PostAsync("api/nozzle/updateNozzleStatus", requesStr);
  43. }
  44. }
  45. }