[성현모] CPMeta 라이브러리, 예제 추가

This commit is contained in:
SHM
2025-10-31 11:49:13 +09:00
parent b78e5a23ea
commit 3d5e790057
12 changed files with 378 additions and 0 deletions

View File

@ -0,0 +1,67 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace CPMeta
{
public class RestAPI
{
public static async Task<RESPONSE> PostAsync<REQUEST,RESPONSE>(string Url, REQUEST body) where REQUEST : class where RESPONSE : class
{
try
{
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
if (body != null)
{
var jsonBody = JsonConvert.SerializeObject(body);
var contents = new StringContent(jsonBody, Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
client.Timeout = TimeSpan.FromMilliseconds(3000);
var response = await client.PostAsync(Url, contents);
var resContentStr = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<RESPONSE>(resContentStr);
}
}
catch (Exception e)
{
Console.WriteLine("PostAsync Error");
}
return default(RESPONSE);
}
public static async Task<RESPONSE> GetAsync<RESPONSE>(string Url)
{
try
{
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
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();
return JsonConvert.DeserializeObject<RESPONSE>(resContentStr);
}
catch(Exception e)
{
Console.WriteLine("GetAsync Error");
}
return default(RESPONSE);
}
}
}