HttpTool.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections;
  2. using UnityEngine;
  3. using System;
  4. using UnityEngine.Networking;
  5. using System.Text;
  6. using GFGGame.Launcher;
  7. namespace GFGGame
  8. {
  9. /// <summary>
  10. /// Http Request SDK
  11. /// </summary>
  12. public class HttpTool : SingletonMonoBase<HttpTool>
  13. {
  14. public void Get(string prefixUrl, string methodName, Action<string> callback)
  15. {
  16. StartCoroutine(GetRequest(prefixUrl, methodName, callback));
  17. }
  18. public IEnumerator GetRequest(string prefixUrl, string methodName, Action<string> callback)
  19. {
  20. string url = prefixUrl + methodName;
  21. Debug.Log("get url : " + url);
  22. using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
  23. {
  24. webRequest.timeout = LauncherConfig.HTTP_GET_TIME_OUT;
  25. yield return webRequest.SendWebRequest();
  26. ResultHandler(webRequest, callback, prefixUrl, () =>
  27. {
  28. Get(prefixUrl, methodName, callback);
  29. });
  30. }
  31. }
  32. //jsonString 为json字符串,post提交的数据包为json
  33. public void Post(string prefixUrl, string methodName, string jsonString, Action<string> callback)
  34. {
  35. StartCoroutine(PostRequest(prefixUrl, methodName, jsonString, callback));
  36. }
  37. public IEnumerator PostRequest(string prefixUrl, string methodName, string jsonString, Action<string> callback)
  38. {
  39. string url = prefixUrl + methodName;
  40. Debug.Log(string.Format("post url:{0} postData:{1}", url, jsonString));
  41. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  42. {
  43. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  44. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  45. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  46. webRequest.timeout = LauncherConfig.HTTP_POST_TIME_OUT;
  47. //http header 的内容
  48. webRequest.SetRequestHeader("Content-Type", "application/json");
  49. // webRequest.timeout;
  50. yield return webRequest.SendWebRequest();
  51. ResultHandler(webRequest, callback, jsonString, () =>
  52. {
  53. Post(prefixUrl, methodName, jsonString, callback);
  54. });
  55. }
  56. }
  57. public void ResultHandler(UnityWebRequest webRequest, Action<string> callback, string tag, Action retryCall)
  58. {
  59. string paramCallback = null;
  60. if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  61. {
  62. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  63. Alert.Show("连接服务器失败:\n请检查网络和服务器状态")
  64. .SetRightButton(true, "重试", (object data) =>
  65. {
  66. retryCall();
  67. });
  68. }
  69. else
  70. {
  71. Debug.Log("from server " + webRequest.downloadHandler.text + "\nby " + tag);
  72. paramCallback = webRequest.downloadHandler.text;
  73. //paramCallback = System.Text.Encoding.UTF8.GetString(webRequest.downloadHandler.data, 3, webRequest.downloadHandler.data.Length - 3);
  74. callback?.Invoke(paramCallback);
  75. }
  76. }
  77. }
  78. }