mirror of
https://github.com/jie65535/GrasscutterCommandGenerator.git
synced 2025-06-08 23:19:14 +08:00
54 lines
1.6 KiB
C#
54 lines
1.6 KiB
C#
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
|
|
{
|
|
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);
|
|
var responseString = await responseMessage.Content.ReadAsStringAsync();
|
|
return JsonConvert.DeserializeObject<T>(responseString);
|
|
}
|
|
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);
|
|
var responseString = await responseMessage.Content.ReadAsStringAsync();
|
|
return JsonConvert.DeserializeObject<T>(responseString);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.InnerException != null)
|
|
throw ex.InnerException;
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|