[성현모] RestAPI 클래스 수정

This commit is contained in:
SHM
2025-11-03 08:58:51 +09:00
parent 3d5e790057
commit 3ba63ccb68

View File

@ -10,22 +10,27 @@ namespace CPMeta
{ {
public class RestAPI public class RestAPI
{ {
private static HttpClient RequestClient = new HttpClient();
private static HttpClient ResponseClient = new HttpClient();
static RestAPI()
{
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
RequestClient.Timeout = TimeSpan.FromMilliseconds(3000);
ResponseClient.Timeout = TimeSpan.FromMilliseconds(3000);
}
public static async Task<RESPONSE> PostAsync<REQUEST,RESPONSE>(string Url, REQUEST body) where REQUEST : class where RESPONSE : class public static async Task<RESPONSE> PostAsync<REQUEST,RESPONSE>(string Url, REQUEST body) where REQUEST : class where RESPONSE : class
{ {
try try
{ {
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
if (body != null) if (body != null)
{ {
var jsonBody = JsonConvert.SerializeObject(body); var jsonBody = JsonConvert.SerializeObject(body);
var contents = new StringContent(jsonBody, Encoding.UTF8, "application/json"); var contents = new StringContent(jsonBody, Encoding.UTF8, "application/json");
HttpClient client = new HttpClient(); var response = await RequestClient.PostAsync(Url, contents);
client.Timeout = TimeSpan.FromMilliseconds(3000);
var response = await client.PostAsync(Url, contents);
var resContentStr = await response.Content.ReadAsStringAsync(); var resContentStr = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<RESPONSE>(resContentStr); return JsonConvert.DeserializeObject<RESPONSE>(resContentStr);
@ -34,6 +39,7 @@ namespace CPMeta
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine("PostAsync Error"); Console.WriteLine("PostAsync Error");
Console.WriteLine(e.Message);
} }
return default(RESPONSE); return default(RESPONSE);
@ -43,13 +49,7 @@ namespace CPMeta
{ {
try try
{ {
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; var response = await ResponseClient.GetAsync(Url);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpClient client = new HttpClient();
client.Timeout = TimeSpan.FromMilliseconds(3000);
var response = await client.GetAsync(Url);
var resContentStr = await response.Content.ReadAsStringAsync(); var resContentStr = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<RESPONSE>(resContentStr); return JsonConvert.DeserializeObject<RESPONSE>(resContentStr);
@ -58,6 +58,7 @@ namespace CPMeta
catch(Exception e) catch(Exception e)
{ {
Console.WriteLine("GetAsync Error"); Console.WriteLine("GetAsync Error");
Console.WriteLine(e.Message);
} }
return default(RESPONSE); return default(RESPONSE);