[성현모] NetStandard Dll 추가
This commit is contained in:
92
Projects/NetStandard/KmsProxy/RestAPI.cs
Normal file
92
Projects/NetStandard/KmsProxy/RestAPI.cs
Normal file
@ -0,0 +1,92 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KmsProxy
|
||||
{
|
||||
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(10000);
|
||||
ResponseClient.Timeout = TimeSpan.FromMilliseconds(10000);
|
||||
}
|
||||
|
||||
public static async Task<RESPONSE> PostAsync<REQUEST, RESPONSE>(string Url, REQUEST body) where REQUEST : class where RESPONSE : class
|
||||
{
|
||||
try
|
||||
{
|
||||
if (body != null)
|
||||
{
|
||||
var jsonBody = JsonConvert.SerializeObject(body);
|
||||
var contents = new StringContent(jsonBody, Encoding.UTF8, "application/json");
|
||||
|
||||
var response = await RequestClient.PostAsync(Url, contents);
|
||||
|
||||
var resContentStr = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<RESPONSE>(resContentStr);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("PostAsync Error");
|
||||
Console.WriteLine(e.Message);
|
||||
}
|
||||
|
||||
return default(RESPONSE);
|
||||
}
|
||||
|
||||
public static async Task<RESPONSE> GetAsync<RESPONSE>(string Url)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await ResponseClient.GetAsync(Url);
|
||||
|
||||
var resContentStr = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<RESPONSE>(resContentStr);
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("GetAsync Error");
|
||||
Console.WriteLine(e.Message);
|
||||
}
|
||||
|
||||
return default(RESPONSE);
|
||||
}
|
||||
|
||||
public static async Task<RESPONSE> PutAsync<REQUEST, RESPONSE>(string Url, REQUEST body) where REQUEST : class where RESPONSE : class
|
||||
{
|
||||
try
|
||||
{
|
||||
if (body != null)
|
||||
{
|
||||
var jsonBody = JsonConvert.SerializeObject(body);
|
||||
var contents = new StringContent(jsonBody, Encoding.UTF8, "application/json");
|
||||
|
||||
var response = await RequestClient.PutAsync(Url, contents);
|
||||
|
||||
var resContentStr = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<RESPONSE>(resContentStr);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("PostAsync Error");
|
||||
Console.WriteLine(e.Message);
|
||||
}
|
||||
|
||||
return default(RESPONSE);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user