GrasscutterCommandGenerator/Source/GrasscutterTools/Utils/HttpHelper.cs
2022-05-14 00:07:28 +08:00

85 lines
2.8 KiB
C#

/**
* Grasscutter Tools
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
**/
using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace GrasscutterTools.Utils
{
public static class HttpHelper
{
private static readonly HttpClient httpClient = new HttpClient();
static HttpHelper()
{
ServicePointManager.ServerCertificateValidationCallback = (_, _1, _2, _3) => true;
}
public static async Task<T> GetAsync<T>(string url)
{
try
{
var responseMessage = await httpClient.GetAsync(url);
if (responseMessage.IsSuccessStatusCode)
{
var responseString = await responseMessage.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(responseString);
}
else
{
throw new HttpRequestException(responseMessage.ReasonPhrase);
}
}
catch (Exception ex)
{
if (ex.InnerException != null)
throw ex.InnerException;
throw;
}
}
public static async Task<T> PostAsync<T>(string url, object obj)
{
try
{
var content = new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json");
var responseMessage = await httpClient.PostAsync(url, content);
if (responseMessage.IsSuccessStatusCode)
{
var responseString = await responseMessage.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(responseString);
}
else
{
throw new HttpRequestException(responseMessage.ReasonPhrase);
}
}
catch (Exception ex)
{
if (ex.InnerException != null)
throw ex.InnerException;
throw;
}
}
}
}