Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

169 řádky
5.5 KiB

  1. using Cysharp.Threading.Tasks;
  2. using System;
  3. using System.Text;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. public class HttpManager : MonoBehaviour
  7. {
  8. static public int isZhengShi = 3;
  9. string urlHead = GetUrlHead(isZhengShi);
  10. static string GetUrlHead(int _isZhengShi)
  11. {
  12. string urlHead;
  13. switch (_isZhengShi)
  14. {
  15. case 0:
  16. urlHead = "http://192.168.0.113:8000/yogame/";
  17. break;
  18. case 1:
  19. urlHead = "http://cat.xiuxiangames.com/yogame/";
  20. break;
  21. case 2:
  22. urlHead = "https://cat.xiuxiangames.com/yogameuat/";
  23. break;
  24. case 3:
  25. urlHead = "https://zshi.gamesverses.com/zsmxj/";
  26. break;
  27. default:
  28. urlHead = "http://192.168.0.113:8000/yogame/"; // 默认地址
  29. break;
  30. }
  31. return urlHead;
  32. }
  33. // 单例模式
  34. public static HttpManager Instance { get; private set; }
  35. private void Awake()
  36. {
  37. // 实现单例模式
  38. if (Instance != null && Instance != this)
  39. {
  40. Destroy(gameObject);
  41. }
  42. else
  43. {
  44. Instance = this;
  45. DontDestroyOnLoad(gameObject);
  46. }
  47. }
  48. public async UniTask<T> RequestPost<F, T>(string _url, F req) where F : WWWRequest, new() where T : WWWResponse, new()
  49. {
  50. string url = urlHead + _url;
  51. string json = req.ToJson();
  52. Debug.Log("Request URL: " + url);
  53. Debug.Log("Request Body (JSON): " + json);
  54. Debug.Log("Request Body Length: " + Encoding.UTF8.GetBytes(json).Length);
  55. try
  56. {
  57. // 使用 UnityWebRequest.Post 初始化请求对象
  58. using (UnityWebRequest www = UnityWebRequest.Post(url, ""))
  59. {
  60. www.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
  61. www.SetRequestHeader("Accept", "*/*");
  62. // 手动设置 UploadHandler 以发送 JSON 数据
  63. www.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(json))
  64. {
  65. contentType = "application/json"
  66. };
  67. www.downloadHandler = new DownloadHandlerBuffer();
  68. try
  69. {
  70. UnityWebRequest op = await www.SendWebRequest().ToUniTask();
  71. Debug.Log("HTTP Status Code: " + www.responseCode);
  72. if (op.result != UnityWebRequest.Result.Success)
  73. {
  74. Debug.LogError("Error: " + op.error);
  75. return null;
  76. }
  77. if (string.IsNullOrEmpty(op.downloadHandler.text))
  78. {
  79. Debug.LogError("Empty or null response received.");
  80. return null;
  81. }
  82. Debug.Log("Response Text: " + op.downloadHandler.text);
  83. T response = JsonUtility.FromJson<T>(op.downloadHandler.text);
  84. return response;
  85. }
  86. catch (Exception ex)
  87. {
  88. Debug.LogError("Exception during request: " + ex);
  89. return null;
  90. }
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. Debug.LogError("Error creating UnityWebRequest: " + ex.Message);
  96. return null;
  97. }
  98. }
  99. public async UniTask<T> RequestGet<F, T>(string _url, F req) where F : WWWRequest, new() where T : WWWResponse, new()
  100. {
  101. string url = urlHead + _url;
  102. // 将请求对象序列化为 JSON,并进行 URL 编码
  103. string json = req.ToJson();
  104. Debug.Log("Request URL: " + url);
  105. Debug.Log("Request Body (JSON): " + json);
  106. string encodedQuery = req.ToQueryString();
  107. url += "?" + encodedQuery;
  108. Debug.Log("Request URL with Query Parameter: " + url);
  109. try
  110. {
  111. // 使用 UnityWebRequest.Get 初始化请求对象
  112. using (UnityWebRequest www = UnityWebRequest.Get(url))
  113. {
  114. www.SetRequestHeader("Accept", "*/*");
  115. try
  116. {
  117. UnityWebRequest op = await www.SendWebRequest().ToUniTask();
  118. Debug.Log("HTTP Status Code: " + www.responseCode);
  119. if (op.result != UnityWebRequest.Result.Success)
  120. {
  121. Debug.LogError("Error: " + op.error);
  122. return null;
  123. }
  124. if (string.IsNullOrEmpty(op.downloadHandler.text))
  125. {
  126. Debug.LogError("Empty or null response received.");
  127. return null;
  128. }
  129. Debug.Log("Response Text: " + op.downloadHandler.text);
  130. T response = JsonUtility.FromJson<T>(op.downloadHandler.text);
  131. return response;
  132. }
  133. catch (Exception ex)
  134. {
  135. Debug.LogError("Exception during request: " + ex);
  136. return null;
  137. }
  138. }
  139. }
  140. catch (Exception ex)
  141. {
  142. Debug.LogError("Error creating UnityWebRequest: " + ex.Message);
  143. return null;
  144. }
  145. }
  146. }