优化基础 GET 和 POST

This commit is contained in:
milimoe 2026-01-03 21:29:53 +08:00
parent ea22adf9cd
commit 1121a0d0b5
Signed by: milimoe
GPG Key ID: 9554D37E4B8991D0
2 changed files with 54 additions and 12 deletions

View File

@ -256,8 +256,10 @@ namespace Milimoe.FunGame.Core.Api.Utility
/// <returns></returns>
public static T? JsonDeserializeFromHashtable<T>(Hashtable hashtable, string key, JsonSerializerOptions options) => Service.JsonManager.GetObject<T>(hashtable, key, options);
// 创建一个静态 HttpClient 实例,供整个应用程序生命周期使用
private static readonly HttpClient client = new();
/// <summary>
/// 创建一个静态 HttpClient 实例,供整个应用程序生命周期使用
/// </summary>
public static HttpClient HttpClient { get; } = new();
/// <summary>
/// 发送 GET 请求
@ -266,12 +268,22 @@ namespace Milimoe.FunGame.Core.Api.Utility
/// <returns></returns>
public static async Task<T?> HttpGet<T>(string url)
{
HttpResponseMessage response = await client.GetAsync(url);
HttpResponseMessage response = await HttpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
T? result = JsonDeserialize<T>(content);
return result;
try
{
return JsonDeserialize<T>(content);
}
catch
{
if (typeof(T) == typeof(string))
{
return (T)(object)content;
}
}
return default;
}
/// <summary>
@ -284,12 +296,22 @@ namespace Milimoe.FunGame.Core.Api.Utility
public static async Task<T?> HttpPost<T>(string url, string json)
{
HttpContent content = new StringContent(json, General.DefaultEncoding, "application/json");
HttpResponseMessage response = await client.PostAsync(url, content);
HttpResponseMessage response = await HttpClient.PostAsync(url, content);
response.EnsureSuccessStatusCode();
string read = await response.Content.ReadAsStringAsync();
T? result = JsonDeserialize<T>(read);
return result;
try
{
return JsonDeserialize<T>(read);
}
catch
{
if (typeof(T) == typeof(string))
{
return (T)(object)content;
}
}
return default;
}
}

View File

@ -89,8 +89,18 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
T? result = NetworkUtility.JsonDeserialize<T>(content);
return result;
try
{
return NetworkUtility.JsonDeserialize<T>(content);
}
catch
{
if (typeof(T) == typeof(string))
{
return (T)(object)content;
}
}
return default;
}
/// <summary>
@ -107,8 +117,18 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
response.EnsureSuccessStatusCode();
string read = await response.Content.ReadAsStringAsync();
T? result = NetworkUtility.JsonDeserialize<T>(read);
return result;
try
{
return NetworkUtility.JsonDeserialize<T>(read);
}
catch
{
if (typeof(T) == typeof(string))
{
return (T)(object)content;
}
}
return default;
}
public void AddSocketObjectHandler(Action<SocketObject> method)