155 lines
5.8 KiB
C#
155 lines
5.8 KiB
C#
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http.Json;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SystemX.Core.Communication
|
|
{
|
|
public class Http
|
|
{
|
|
/// <summary>
|
|
/// PostJsonAsync
|
|
/// </summary>
|
|
/// <param name="url">https://127.0.0.1:443</param>
|
|
/// <param name="timeOutSeconds">Range 5~30 secconds</param>
|
|
public virtual async Task<RESPONSE?> PostJsonAsync<REQUEST, RESPONSE>(string url, REQUEST request, string bearerToken = "", short timeOutSeconds = 10) where REQUEST : class where RESPONSE : class
|
|
{
|
|
RESPONSE? response = default(RESPONSE);
|
|
Guid guid = Guid.NewGuid();
|
|
|
|
using (HttpClient httpClient = new HttpClient(GetClientHandler()))
|
|
{
|
|
var timeOutSec = SetTimeout(timeOutSeconds);
|
|
httpClient.Timeout = new TimeSpan(0, 0, timeOutSec);
|
|
httpClient.BaseAddress = new Uri($"{url}");
|
|
|
|
if(string.IsNullOrEmpty(bearerToken) == false)
|
|
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", $"{bearerToken}");
|
|
|
|
int retry = 0;
|
|
while (true)
|
|
{
|
|
await Task.Delay(1);
|
|
try
|
|
{
|
|
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<RESPONSE>();
|
|
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// GetJsonAsnyc
|
|
/// </summary>
|
|
/// <param name="url">https://127.0.0.1:443</param>
|
|
/// <param name="timeOutSeconds">Range 5~30 secconds</param>
|
|
public virtual async Task<RESPONSE?> GetJsonAsync<RESPONSE>(string url, string bearerToken = "", 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}");
|
|
|
|
if (string.IsNullOrEmpty(bearerToken) == false)
|
|
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", $"{bearerToken}");
|
|
|
|
LogXnet.WriteLine($"[GET] Request({guid})::{url}", LogXLabel.HTTP);
|
|
|
|
DateTime requestTime = DateTime.Now;
|
|
response = await httpClient.GetFromJsonAsync<RESPONSE>(url);
|
|
|
|
LogXnet.WriteLine($"[GET] Rseponse({guid}) ({(DateTime.Now - requestTime).TotalSeconds} sec)::{url}", LogXLabel.HTTP);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogXnet.WriteLine(e);
|
|
}
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
public virtual async Task<Stream?> GetStreamAsync(string url, string bearerToken = "", short timeOutSeconds = 10)
|
|
{
|
|
Guid guid = Guid.NewGuid();
|
|
|
|
Stream stream = null;
|
|
|
|
using (HttpClient httpClient = new HttpClient(GetClientHandler()))
|
|
{
|
|
try
|
|
{
|
|
var timeOutSec = SetTimeout(timeOutSeconds);
|
|
httpClient.Timeout = new TimeSpan(0, 0, timeOutSec);
|
|
httpClient.BaseAddress = new Uri($"{url}");
|
|
|
|
if (string.IsNullOrEmpty(bearerToken) == false)
|
|
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", $"{bearerToken}");
|
|
|
|
LogXnet.WriteLine($"[GET] Request({guid})::{url}", LogXLabel.HTTP);
|
|
|
|
DateTime requestTime = DateTime.Now;
|
|
var response = await httpClient.GetAsync(url);
|
|
stream = await response.Content.ReadAsStreamAsync();
|
|
|
|
LogXnet.WriteLine($"[GET] Rseponse({guid}) ({(DateTime.Now - requestTime).TotalSeconds} sec)::{url}", LogXLabel.HTTP);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogXnet.WriteLine(e);
|
|
}
|
|
}
|
|
|
|
return stream;
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|