diff --git a/UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.Http.cs b/UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.Http.cs
index a0023ce0..40a0fab1 100644
--- a/UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.Http.cs
+++ b/UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.Http.cs
@@ -1,6 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
+using System.Threading;
+using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
@@ -14,6 +16,103 @@ namespace TEngine
///
public static class Http
{
+ ///
+ /// GET请求与获取结果。
+ ///
+ /// 网络URL。
+ /// 超时时间。
+ /// 请求结果。
+ public static async UniTask Get(string url, float timeout = 5f)
+ {
+ var cts = new CancellationTokenSource();
+ cts.CancelAfterSlim(TimeSpan.FromSeconds(timeout));
+
+ using UnityWebRequest unityWebRequest = UnityWebRequest.Get(url);
+ return await SendWebRequest(unityWebRequest, cts);
+ }
+
+ ///
+ /// Post请求与获取结果.
+ ///
+ /// 网络URL。
+ /// Post数据。
+ /// 超时时间。
+ /// 请求结果。
+ public static async UniTask Post(string url, string postData, float timeout = 5f)
+ {
+ var cts = new CancellationTokenSource();
+ cts.CancelAfterSlim(TimeSpan.FromSeconds(timeout));
+
+ using UnityWebRequest unityWebRequest = UnityWebRequest.Post(url, postData);
+ return await SendWebRequest(unityWebRequest, cts);
+ }
+
+ ///
+ /// Post请求与获取结果.
+ ///
+ /// 网络URL。
+ /// Post数据。
+ /// 超时时间。
+ /// 请求结果。
+ public static async UniTask Post(string url, Dictionary formFields, float timeout = 5f)
+ {
+ var cts = new CancellationTokenSource();
+ cts.CancelAfterSlim(TimeSpan.FromSeconds(timeout));
+
+ using UnityWebRequest unityWebRequest = UnityWebRequest.Post(url, formFields);
+ return await SendWebRequest(unityWebRequest, cts);
+ }
+
+ ///
+ /// Post请求与获取结果.
+ ///
+ /// 网络URL。
+ /// Post数据。
+ /// 超时时间。
+ /// 请求结果。
+ public static async UniTask Post(string url, WWWForm formData, float timeout = 5f)
+ {
+ var cts = new CancellationTokenSource();
+ cts.CancelAfterSlim(TimeSpan.FromSeconds(timeout));
+
+ using UnityWebRequest unityWebRequest = UnityWebRequest.Post(url, formData);
+ return await SendWebRequest(unityWebRequest, cts);
+ }
+
+ ///
+ /// 抛出网络请求。
+ ///
+ /// UnityWebRequest。
+ /// CancellationTokenSource。
+ /// 请求结果。
+ public static async UniTask SendWebRequest(UnityWebRequest unityWebRequest, CancellationTokenSource cts)
+ {
+ try
+ {
+ var (isCanceled, _) = await unityWebRequest.SendWebRequest().WithCancellation(cts.Token).SuppressCancellationThrow();
+ if (isCanceled)
+ {
+ Log.Warning($"HttpPost {unityWebRequest.url} be canceled!");
+ unityWebRequest.Dispose();
+ return string.Empty;
+ }
+ }
+ catch (OperationCanceledException ex)
+ {
+ if (ex.CancellationToken == cts.Token)
+ {
+ Log.Warning("HttpPost Timeout");
+ unityWebRequest.Dispose();
+ return string.Empty;
+ }
+ }
+
+ string ret = unityWebRequest.downloadHandler.text;
+ unityWebRequest.Dispose();
+ return ret;
+ }
+
+
///
/// 发起HTTP GET请求。
///
@@ -79,7 +178,7 @@ namespace TEngine
}
#region Private Coroutine Implementations
-
+
///
/// GET请求协程实现。
///
@@ -117,6 +216,7 @@ namespace TEngine
{
texture2D = downloadTexture.texture;
}
+
actionResult?.Invoke(texture2D);
}
@@ -159,8 +259,8 @@ namespace TEngine
bool flag = uwr.result == UnityWebRequest.Result.Success;
actionResult?.Invoke(flag);
}
-
+
#endregion
}
}
-}
+}
\ No newline at end of file