From 5b40cfbd390f93747b3cf903b9b50e561100b473 Mon Sep 17 00:00:00 2001 From: liguoyang Date: Wed, 21 Feb 2024 15:15:12 +0800 Subject: [PATCH] =?UTF-8?q?fix=20sap-api=E5=87=AD=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/Implements/JinkoSapService.cs | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Src/Dlhis.ScheduledTask/SyncSapInfo/Services/Implements/JinkoSapService.cs b/Src/Dlhis.ScheduledTask/SyncSapInfo/Services/Implements/JinkoSapService.cs index 7e2eac25..62ebf1f6 100644 --- a/Src/Dlhis.ScheduledTask/SyncSapInfo/Services/Implements/JinkoSapService.cs +++ b/Src/Dlhis.ScheduledTask/SyncSapInfo/Services/Implements/JinkoSapService.cs @@ -1,4 +1,6 @@ -using System.Text; +using System.Net.Http.Headers; +using System.Net.Http; +using System.Text; using Dlhis.ScheduledTask.SyncSapInfo.Dto; using Dlhis.ScheduledTask.SyncSapInfo.Response; using Dlhis.Sys.Api; @@ -8,22 +10,33 @@ namespace Dlhis.ScheduledTask.SyncSapInfo.Services.Implements; public class JinkoSapService : IJinkoSapService { - private readonly HttpClient _httpClient; private readonly ILogger _logger; + private readonly IHttpClientFactory _httpClientFactory; private readonly ITenantApi _tenantApi; public JinkoSapService(IHttpClientFactory httpClientFactory, ILogger logger, ITenantApi tenantApi) { - _httpClient = httpClientFactory.CreateClient("jinko:sap"); _logger = logger; + _httpClientFactory = httpClientFactory; _tenantApi = tenantApi; } + private HttpClient GetHttpClient() + { + var httpClient = _httpClientFactory.CreateClient("jinko:sap"); + httpClient.Timeout = TimeSpan.FromMinutes(10); + var credentials = string.Format("{0}:{1}", "EHS_USER", "y3R1hd2h*RICAC"); + var headerValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials)); + httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", headerValue); + + return httpClient; + } + public async Task GetSapOrganizationsAsync() { string apiUrl = "http://sappidap.jinkosolar.cn:51000/RESTAdapter/Rest/EHR_ORGANIZATION/"; - var response = await _httpClient.PostAsync(apiUrl, new StringContent("{}", Encoding.UTF8, "application/json")); + var response = await GetHttpClient().PostAsync(apiUrl, new StringContent("{}", Encoding.UTF8, "application/json")); response.EnsureSuccessStatusCode(); string responseJson = await response.Content.ReadAsStringAsync(); @@ -36,7 +49,7 @@ public class JinkoSapService : IJinkoSapService { string apiUrl = "http://sappidap.jinkosolar.cn:51000/RESTAdapter/Rest/EHR_POSITION/"; - var response = await _httpClient.PostAsync(apiUrl, new StringContent("{}", Encoding.UTF8, "application/json")); + var response = await GetHttpClient().PostAsync(apiUrl, new StringContent("{}", Encoding.UTF8, "application/json")); response.EnsureSuccessStatusCode(); string responseJson = await response.Content.ReadAsStringAsync(); @@ -75,7 +88,7 @@ public class JinkoSapService : IJinkoSapService }; var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"); - var response = await _httpClient.PostAsync(apiUrl, content); + var response = await GetHttpClient().PostAsync(apiUrl, content); response.EnsureSuccessStatusCode(); var jsonResponse = await response.Content.ReadAsStringAsync();