mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2026-01-19 14:08:22 +00:00
优化基础 GET 和 POST
This commit is contained in:
parent
ea22adf9cd
commit
1121a0d0b5
@ -256,8 +256,10 @@ namespace Milimoe.FunGame.Core.Api.Utility
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static T? JsonDeserializeFromHashtable<T>(Hashtable hashtable, string key, JsonSerializerOptions options) => Service.JsonManager.GetObject<T>(hashtable, key, options);
|
public static T? JsonDeserializeFromHashtable<T>(Hashtable hashtable, string key, JsonSerializerOptions options) => Service.JsonManager.GetObject<T>(hashtable, key, options);
|
||||||
|
|
||||||
// 创建一个静态 HttpClient 实例,供整个应用程序生命周期使用
|
/// <summary>
|
||||||
private static readonly HttpClient client = new();
|
/// 创建一个静态 HttpClient 实例,供整个应用程序生命周期使用
|
||||||
|
/// </summary>
|
||||||
|
public static HttpClient HttpClient { get; } = new();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送 GET 请求
|
/// 发送 GET 请求
|
||||||
@ -266,12 +268,22 @@ namespace Milimoe.FunGame.Core.Api.Utility
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task<T?> HttpGet<T>(string url)
|
public static async Task<T?> HttpGet<T>(string url)
|
||||||
{
|
{
|
||||||
HttpResponseMessage response = await client.GetAsync(url);
|
HttpResponseMessage response = await HttpClient.GetAsync(url);
|
||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
string content = await response.Content.ReadAsStringAsync();
|
string content = await response.Content.ReadAsStringAsync();
|
||||||
T? result = JsonDeserialize<T>(content);
|
try
|
||||||
return result;
|
{
|
||||||
|
return JsonDeserialize<T>(content);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
if (typeof(T) == typeof(string))
|
||||||
|
{
|
||||||
|
return (T)(object)content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -284,12 +296,22 @@ namespace Milimoe.FunGame.Core.Api.Utility
|
|||||||
public static async Task<T?> HttpPost<T>(string url, string json)
|
public static async Task<T?> HttpPost<T>(string url, string json)
|
||||||
{
|
{
|
||||||
HttpContent content = new StringContent(json, General.DefaultEncoding, "application/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();
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
string read = await response.Content.ReadAsStringAsync();
|
string read = await response.Content.ReadAsStringAsync();
|
||||||
T? result = JsonDeserialize<T>(read);
|
try
|
||||||
return result;
|
{
|
||||||
|
return JsonDeserialize<T>(read);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
if (typeof(T) == typeof(string))
|
||||||
|
{
|
||||||
|
return (T)(object)content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return default;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -89,8 +89,18 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
|||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
string content = await response.Content.ReadAsStringAsync();
|
string content = await response.Content.ReadAsStringAsync();
|
||||||
T? result = NetworkUtility.JsonDeserialize<T>(content);
|
try
|
||||||
return result;
|
{
|
||||||
|
return NetworkUtility.JsonDeserialize<T>(content);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
if (typeof(T) == typeof(string))
|
||||||
|
{
|
||||||
|
return (T)(object)content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -107,8 +117,18 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
|||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
string read = await response.Content.ReadAsStringAsync();
|
string read = await response.Content.ReadAsStringAsync();
|
||||||
T? result = NetworkUtility.JsonDeserialize<T>(read);
|
try
|
||||||
return result;
|
{
|
||||||
|
return NetworkUtility.JsonDeserialize<T>(read);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
if (typeof(T) == typeof(string))
|
||||||
|
{
|
||||||
|
return (T)(object)content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddSocketObjectHandler(Action<SocketObject> method)
|
public void AddSocketObjectHandler(Action<SocketObject> method)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user