using Cysharp.Threading.Tasks; using System; using System.Text; using UnityEngine; using UnityEngine.Networking; public class HttpManager : MonoBehaviour { static public int isZhengShi = 3; string urlHead = GetUrlHead(isZhengShi); static string GetUrlHead(int _isZhengShi) { string urlHead; switch (_isZhengShi) { case 0: urlHead = "http://192.168.0.113:8000/yogame/"; break; case 1: urlHead = "http://cat.xiuxiangames.com/yogame/"; break; case 2: urlHead = "https://cat.xiuxiangames.com/yogameuat/"; break; case 3: urlHead = "https://zshi.gamesverses.com/zsmxj/"; break; default: urlHead = "http://192.168.0.113:8000/yogame/"; // 默认地址 break; } return urlHead; } // 单例模式 public static HttpManager Instance { get; private set; } private void Awake() { // 实现单例模式 if (Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; DontDestroyOnLoad(gameObject); } } public async UniTask RequestPost(string _url, F req) where F : WWWRequest, new() where T : WWWResponse, new() { string url = urlHead + _url; string json = req.ToJson(); Debug.Log("Request URL: " + url); Debug.Log("Request Body (JSON): " + json); Debug.Log("Request Body Length: " + Encoding.UTF8.GetBytes(json).Length); try { // 使用 UnityWebRequest.Post 初始化请求对象 using (UnityWebRequest www = UnityWebRequest.Post(url, "")) { www.SetRequestHeader("Content-Type", "application/json;charset=utf-8"); www.SetRequestHeader("Accept", "*/*"); // 手动设置 UploadHandler 以发送 JSON 数据 www.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(json)) { contentType = "application/json" }; www.downloadHandler = new DownloadHandlerBuffer(); try { UnityWebRequest op = await www.SendWebRequest().ToUniTask(); Debug.Log("HTTP Status Code: " + www.responseCode); if (op.result != UnityWebRequest.Result.Success) { Debug.LogError("Error: " + op.error); return null; } if (string.IsNullOrEmpty(op.downloadHandler.text)) { Debug.LogError("Empty or null response received."); return null; } Debug.Log("Response Text: " + op.downloadHandler.text); T response = JsonUtility.FromJson(op.downloadHandler.text); return response; } catch (Exception ex) { Debug.LogError("Exception during request: " + ex); return null; } } } catch (Exception ex) { Debug.LogError("Error creating UnityWebRequest: " + ex.Message); return null; } } public async UniTask RequestGet(string _url, F req) where F : WWWRequest, new() where T : WWWResponse, new() { string url = urlHead + _url; // 将请求对象序列化为 JSON,并进行 URL 编码 string json = req.ToJson(); Debug.Log("Request URL: " + url); Debug.Log("Request Body (JSON): " + json); string encodedQuery = req.ToQueryString(); url += "?" + encodedQuery; Debug.Log("Request URL with Query Parameter: " + url); try { // 使用 UnityWebRequest.Get 初始化请求对象 using (UnityWebRequest www = UnityWebRequest.Get(url)) { www.SetRequestHeader("Accept", "*/*"); try { UnityWebRequest op = await www.SendWebRequest().ToUniTask(); Debug.Log("HTTP Status Code: " + www.responseCode); if (op.result != UnityWebRequest.Result.Success) { Debug.LogError("Error: " + op.error); return null; } if (string.IsNullOrEmpty(op.downloadHandler.text)) { Debug.LogError("Empty or null response received."); return null; } Debug.Log("Response Text: " + op.downloadHandler.text); T response = JsonUtility.FromJson(op.downloadHandler.text); return response; } catch (Exception ex) { Debug.LogError("Exception during request: " + ex); return null; } } } catch (Exception ex) { Debug.LogError("Error creating UnityWebRequest: " + ex.Message); return null; } } }