選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

192 行
6.0 KiB

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