diff --git a/Projects/DLL/SystemX.Core.dll b/Projects/DLL/SystemX.Core.dll index 13fafeb..6413124 100644 Binary files a/Projects/DLL/SystemX.Core.dll and b/Projects/DLL/SystemX.Core.dll differ diff --git a/Projects/SystemX.Core/SystemX.Core/Services/CommonService.cs b/Projects/SystemX.Core/SystemX.Core/Services/CommonService.cs new file mode 100644 index 0000000..c108ac8 --- /dev/null +++ b/Projects/SystemX.Core/SystemX.Core/Services/CommonService.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SystemX.Core.Services +{ + public class CommonService + { + public CommonService() + { + } + + }//end class +} diff --git a/Projects/SystemX.Core/SystemX.Core/Services/HttpService.cs b/Projects/SystemX.Core/SystemX.Core/Services/HttpService.cs new file mode 100644 index 0000000..7aa7cb0 --- /dev/null +++ b/Projects/SystemX.Core/SystemX.Core/Services/HttpService.cs @@ -0,0 +1,163 @@ +using Azure.Core; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http.Json; +using System.Text; +using System.Threading.Tasks; + +namespace SystemX.Core.Services +{ + public class HttpService : CommonService + { + public HttpService() + : base() + { + } + + /// + /// PostJsonAsync + /// + /// https://127.0.0.1:443 + /// Range 5~30 secconds + public virtual async Task PostJsonAsync(string url, REQUEST request, short timeOutSeconds = 5) where REQUEST : class where RESPONSE : class + { + RESPONSE? response = default(RESPONSE); + Guid guid = Guid.NewGuid(); + + using (HttpClient httpClient = new HttpClient(GetClientHandler())) + { + int retry = 0; + while (true) + { + await Task.Delay(1); + try + { + var timeOutSec = SetTimeout(timeOutSeconds); + httpClient.Timeout = new TimeSpan(0, 0, timeOutSec); + httpClient.BaseAddress = new Uri($"{url}"); + + LogXnet.WriteLine($"[POST] Request({guid})::{url}{Environment.NewLine}{request?.ToJson()}", LogXLabel.HTTP); + + DateTime requestTime = DateTime.Now; + var res = await httpClient.PostAsJsonAsync(url, request); + response = await res.Content.ReadFromJsonAsync(); + + LogXnet.WriteLine($"[POST] Rseponse({guid}) ({(DateTime.Now - requestTime).TotalSeconds} sec)::{url}{Environment.NewLine}{response?.ToJson()}", LogXLabel.HTTP); + break; + } + catch (Exception e) + { + LogXnet.WriteLine(e); + retry += 1; + } + + if (retry > 1) + break; + } + } + + return response; + } + + /// + /// GetJsonAsnyc + /// + /// https://127.0.0.1:443 + /// Range 5~30 secconds + public virtual async Task GetJsonAsync(string url, short timeOutSeconds = 10) where RESPONSE : class + { + RESPONSE? response = default(RESPONSE); + Guid guid = Guid.NewGuid(); + + using (HttpClient httpClient = new HttpClient(GetClientHandler())) + { + try + { + var timeOutSec = SetTimeout(timeOutSeconds); + httpClient.Timeout = new TimeSpan(0, 0, timeOutSec); + httpClient.BaseAddress = new Uri($"{url}"); + + LogXnet.WriteLine($"[GET] Request({guid})::{url}", LogXLabel.HTTP); + + DateTime requestTime = DateTime.Now; + response = await httpClient.GetFromJsonAsync(url); + + LogXnet.WriteLine($"[GET] Rseponse({guid}) ({(DateTime.Now - requestTime).TotalSeconds} sec)::{url}", LogXLabel.HTTP); + } + catch (Exception e) + { + LogXnet.WriteLine(e); + } + } + + return response; + } + + /// + /// PutJsonAsnyc + /// + /// https://127.0.0.1:443 + /// Range 5~30 secconds + public virtual async Task PutJsonAsync(string url, REQUEST request, short timeOutSeconds = 5) where REQUEST : class where RESPONSE : class + { + RESPONSE? response = default(RESPONSE); + Guid guid = Guid.NewGuid(); + + using (HttpClient httpClient = new HttpClient(GetClientHandler())) + { + int retry = 0; + while (true) + { + await Task.Delay(1); + try + { + var timeOutSec = SetTimeout(timeOutSeconds); + httpClient.Timeout = new TimeSpan(0, 0, timeOutSec); + httpClient.BaseAddress = new Uri($"{url}"); + + LogXnet.WriteLine($"[PUT] Request({guid})::{url}{Environment.NewLine}{request?.ToJson()}", LogXLabel.HTTP); + + DateTime requestTime = DateTime.Now; + var res = await httpClient.PutAsJsonAsync(url, request); + response = await res.Content.ReadFromJsonAsync(); + + LogXnet.WriteLine($"[PUT] Rseponse({guid}) ({(DateTime.Now - requestTime).TotalSeconds} sec)::{url}{Environment.NewLine}{response?.ToJson()}", LogXLabel.HTTP); + break; + } + catch (Exception e) + { + LogXnet.WriteLine(e); + retry += 1; + } + + if (retry > 1) + break; + } + } + + return response; + } + + protected HttpClientHandler GetClientHandler() + { + HttpClientHandler clientHandler = new HttpClientHandler(); + clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) => + { + return true; + }; + + return clientHandler; + } + + protected short SetTimeout(short timeOutSeconds) + { + short timeoutMin = 5; + short timeoutMax = 30; + + timeOutSeconds = Math.Clamp(timeOutSeconds, timeoutMin, timeoutMax); + + return timeOutSeconds; + } + } +}