HttpTool.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System.Collections;
  2. using UnityEngine;
  3. using System;
  4. using UnityEngine.Networking;
  5. using System.Text;
  6. using GFGGame.Launcher;
  7. using System.Reflection;
  8. using System.Collections.Generic;
  9. namespace GFGGame
  10. {
  11. /// <summary>
  12. /// Http Request SDK
  13. /// </summary>
  14. public class HttpTool : SingletonMonoBase<HttpTool>
  15. {
  16. //最大尝试次数
  17. const int TryTimes = 3;
  18. //存储尝试次数
  19. public Dictionary<string, int> TryTimesDic = new Dictionary<string, int>();
  20. public void Get(string url, Action<string> callback, bool showWrong = true)
  21. {
  22. StartCoroutine(GetRequest(url, callback, showWrong));
  23. }
  24. public void Get(string prefixUrl, string methodName, Action<string> callback, bool showWrong = true)
  25. {
  26. string url = prefixUrl + methodName;
  27. StartCoroutine(GetRequest(url, callback, showWrong));
  28. }
  29. public IEnumerator GetRequest(string url, Action<string> callback, bool showWrong)
  30. {
  31. var key = url;
  32. ET.Log.Debug("get url : " + key);
  33. using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
  34. {
  35. webRequest.timeout = LauncherConfig.HTTP_GET_TIME_OUT;
  36. TryTimesDic.TryGetValue(key, out var times);
  37. times++;
  38. TryTimesDic[key] = times;
  39. yield return webRequest.SendWebRequest();
  40. ResultHandler(webRequest, callback, url, () =>
  41. {
  42. if(times < HttpTool.TryTimes)
  43. {
  44. Get(url, callback, showWrong);
  45. }
  46. else
  47. {
  48. if (showWrong)
  49. {
  50. TryTimesDic.Remove(key);
  51. Alert.Show("连接服务器失败:\n请检查网络和服务器状态")
  52. .SetRightButton(true, "重试", (object data) =>
  53. {
  54. Get(url, callback, showWrong);
  55. });
  56. }
  57. }
  58. });
  59. }
  60. }
  61. public void Post(string url, string body, Action<string> callback = null, string contentType = "application/text", bool showWrong = false)
  62. {
  63. StartCoroutine(PostRequest(url, body, callback, contentType, showWrong));
  64. }
  65. public void Post(string prefixUrl, string methodName, string body, Action<string> callback = null, string contentType = "application/text", bool showWrong = false)
  66. {
  67. string url = prefixUrl + methodName;
  68. Post(url, body, callback, contentType);
  69. }
  70. private IEnumerator PostRequest(string url, string body, Action<string> callback, string contentType, bool showWrong)
  71. {
  72. var key = url + ":" + body;
  73. ET.Log.Debug(string.Format($"post {key}", url, body));
  74. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  75. {
  76. byte[] bodyRaw = Encoding.UTF8.GetBytes(body);
  77. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  78. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  79. webRequest.timeout = LauncherConfig.HTTP_POST_TIME_OUT;
  80. //http header 的内容
  81. webRequest.SetRequestHeader("Content-Type", contentType);
  82. TryTimesDic.TryGetValue(key, out var times);
  83. times++;
  84. TryTimesDic[key] = times;
  85. yield return webRequest.SendWebRequest();
  86. ResultHandler(webRequest, callback, key, () =>
  87. {
  88. if (times < HttpTool.TryTimes)
  89. {
  90. Post(url, body, callback,contentType, showWrong);
  91. }
  92. else
  93. {
  94. if (showWrong)
  95. {
  96. TryTimesDic.Remove(key);
  97. Alert.Show("连接服务器失败:\n请检查网络和服务器状态")
  98. .SetRightButton(true, "重试", (object data) =>
  99. {
  100. Post(url, body, callback, contentType, showWrong);
  101. });
  102. }
  103. }
  104. });
  105. }
  106. }
  107. public void ResultHandler(UnityWebRequest webRequest, Action<string> callback, string tag, Action retryFunc)
  108. {
  109. string paramCallback = null;
  110. if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  111. {
  112. ET.Log.Error(webRequest.error + "\n" + webRequest.downloadHandler.text + "\nby " + tag);
  113. retryFunc?.Invoke();
  114. }
  115. else
  116. {
  117. ET.Log.Debug("from server " + webRequest.downloadHandler.text + "\nby " + tag);
  118. paramCallback = webRequest.downloadHandler.text;
  119. //paramCallback = System.Text.Encoding.UTF8.GetString(webRequest.downloadHandler.data, 3, webRequest.downloadHandler.data.Length - 3);
  120. callback?.Invoke(paramCallback);
  121. }
  122. }
  123. }
  124. }