2025-02-07 16:04:12 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
2025-03-24 13:16:51 +08:00
|
|
|
|
namespace AlicizaX
|
2025-02-07 16:04:12 +08:00
|
|
|
|
{
|
|
|
|
|
public static partial class Utility
|
|
|
|
|
{
|
|
|
|
|
public static partial class Http
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2025-04-23 13:50:17 +08:00
|
|
|
|
/// 发送 GET 请求
|
2025-02-07 16:04:12 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public static async UniTask<string> Get(string url, float timeout = 5f)
|
|
|
|
|
{
|
2025-04-23 13:50:17 +08:00
|
|
|
|
using var request = UnityWebRequest.Get(url);
|
2025-05-15 15:25:11 +08:00
|
|
|
|
return await SendRequest(request);
|
2025-02-07 16:04:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-04-23 13:50:17 +08:00
|
|
|
|
/// 发送 JSON 格式的 POST 请求(服务端需用 [FromBody] 接收)
|
2025-02-07 16:04:12 +08:00
|
|
|
|
/// </summary>
|
2025-04-23 13:50:17 +08:00
|
|
|
|
public static async UniTask<string> PostJson(string url, object jsonData, float timeout = 5f)
|
2025-02-07 16:04:12 +08:00
|
|
|
|
{
|
2025-04-23 13:51:30 +08:00
|
|
|
|
var json = Utility.Json.ToJson(jsonData);
|
2025-04-23 13:50:17 +08:00
|
|
|
|
using var request = CreateJsonPostRequest(url, json);
|
2025-05-15 15:25:11 +08:00
|
|
|
|
return await SendRequest(request);
|
2025-02-07 16:04:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-04-23 13:50:17 +08:00
|
|
|
|
/// 发送表单格式的 POST 请求(x-www-form-urlencoded)
|
2025-02-07 16:04:12 +08:00
|
|
|
|
/// </summary>
|
2025-04-23 13:50:17 +08:00
|
|
|
|
public static async UniTask<string> PostForm(string url, Dictionary<string, string> formData, float timeout = 5f)
|
2025-02-07 16:04:12 +08:00
|
|
|
|
{
|
2025-04-23 13:50:17 +08:00
|
|
|
|
using var request = UnityWebRequest.Post(url, formData);
|
2025-05-15 15:25:11 +08:00
|
|
|
|
return await SendRequest(request);
|
2025-02-07 16:04:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-04-23 13:50:17 +08:00
|
|
|
|
/// 发送多部分表单的 POST 请求(multipart/form-data,支持文件上传)
|
2025-02-07 16:04:12 +08:00
|
|
|
|
/// </summary>
|
2025-04-23 13:50:17 +08:00
|
|
|
|
public static async UniTask<string> PostMultipart(string url, WWWForm formData, float timeout = 5f)
|
|
|
|
|
{
|
|
|
|
|
using var request = UnityWebRequest.Post(url, formData);
|
2025-05-15 15:25:11 +08:00
|
|
|
|
return await SendRequest(request);
|
2025-04-23 13:50:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 16:04:12 +08:00
|
|
|
|
|
2025-04-23 13:50:17 +08:00
|
|
|
|
private static UnityWebRequest CreateJsonPostRequest(string url, string json)
|
|
|
|
|
{
|
|
|
|
|
var request = new UnityWebRequest(url, "POST");
|
|
|
|
|
byte[] jsonBytes = System.Text.Encoding.UTF8.GetBytes(json);
|
|
|
|
|
request.uploadHandler = new UploadHandlerRaw(jsonBytes);
|
|
|
|
|
request.downloadHandler = new DownloadHandlerBuffer();
|
|
|
|
|
request.SetRequestHeader("Content-Type", "application/json");
|
|
|
|
|
return request;
|
2025-02-07 16:04:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 15:25:11 +08:00
|
|
|
|
private static async UniTask<string> SendRequest(UnityWebRequest request)
|
2025-02-07 16:04:12 +08:00
|
|
|
|
{
|
2025-04-27 14:12:30 +08:00
|
|
|
|
string url = request.url; // 提前获取请求地址
|
|
|
|
|
|
2025-02-07 16:04:12 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2025-05-15 15:25:11 +08:00
|
|
|
|
await request.SendWebRequest();
|
2025-04-23 13:50:17 +08:00
|
|
|
|
|
|
|
|
|
if (request.result != UnityWebRequest.Result.Success)
|
2025-02-07 16:04:12 +08:00
|
|
|
|
{
|
2025-04-27 14:12:30 +08:00
|
|
|
|
HandleRequestError(request, url);
|
2025-02-07 16:04:12 +08:00
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
2025-04-23 13:50:17 +08:00
|
|
|
|
|
|
|
|
|
return request.downloadHandler.text;
|
2025-02-07 16:04:12 +08:00
|
|
|
|
}
|
2025-04-27 14:25:52 +08:00
|
|
|
|
catch (UnityWebRequestException e)
|
2025-02-07 16:04:12 +08:00
|
|
|
|
{
|
2025-04-27 14:25:52 +08:00
|
|
|
|
HandleRequestError(request, url);
|
2025-04-23 13:50:17 +08:00
|
|
|
|
return string.Empty;
|
2025-02-07 16:04:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-27 14:12:30 +08:00
|
|
|
|
|
|
|
|
|
private static void HandleRequestError(UnityWebRequest request, string url)
|
|
|
|
|
{
|
2025-05-15 15:27:40 +08:00
|
|
|
|
;
|
2025-04-27 14:12:30 +08:00
|
|
|
|
switch (request.result)
|
|
|
|
|
{
|
|
|
|
|
case UnityWebRequest.Result.ConnectionError:
|
2025-05-15 15:27:40 +08:00
|
|
|
|
Log.Error($"无法访问地址:{url}");
|
2025-04-27 14:12:30 +08:00
|
|
|
|
break;
|
|
|
|
|
case UnityWebRequest.Result.ProtocolError:
|
2025-05-15 15:27:40 +08:00
|
|
|
|
Log.Error($"HTTP协议错误 ({request.responseCode}):{url}\n{request.error}");
|
2025-04-27 14:12:30 +08:00
|
|
|
|
break;
|
|
|
|
|
case UnityWebRequest.Result.DataProcessingError:
|
2025-05-15 15:27:40 +08:00
|
|
|
|
Log.Error($"数据处理错误:{url}\n{request.error}");
|
2025-04-27 14:12:30 +08:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2025-05-15 15:27:40 +08:00
|
|
|
|
Log.Error($"未知网络错误:{url}\n{request.error}");
|
2025-04-27 14:12:30 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-07 16:04:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-24 13:16:51 +08:00
|
|
|
|
}
|