HttpTool.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. //Debug.Log("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. Debug.Log("get url : " + key);
  52. Alert.Show("连接服务器失败:\n请检查网络和服务器状态")
  53. .SetRightButton(true, "重试", (object data) =>
  54. {
  55. Get(url, callback, showWrong);
  56. });
  57. }
  58. }
  59. });
  60. }
  61. }
  62. public void Post(string url, string body, Action<string> callback = null, string contentType = "application/text", bool showWrong = false)
  63. {
  64. StartCoroutine(PostRequest(url, body, callback, contentType, showWrong));
  65. }
  66. public void Post(string prefixUrl, string methodName, string body, Action<string> callback = null, string contentType = "application/text", bool showWrong = false)
  67. {
  68. string url = prefixUrl + methodName;
  69. Post(url, body, callback, contentType);
  70. }
  71. private IEnumerator PostRequest(string url, string body, Action<string> callback, string contentType, bool showWrong)
  72. {
  73. var key = url + ":" + body;
  74. //ET.Log.Debug(string.Format($"post {key}", url, body));
  75. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  76. {
  77. byte[] bodyRaw = Encoding.UTF8.GetBytes(body);
  78. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  79. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  80. webRequest.timeout = LauncherConfig.HTTP_POST_TIME_OUT;
  81. //http header 的内容
  82. webRequest.SetRequestHeader("Content-Type", contentType);
  83. TryTimesDic.TryGetValue(key, out var times);
  84. times++;
  85. TryTimesDic[key] = times;
  86. yield return webRequest.SendWebRequest();
  87. ResultHandler(webRequest, callback, key, () =>
  88. {
  89. if (times < HttpTool.TryTimes)
  90. {
  91. Post(url, body, callback,contentType, showWrong);
  92. }
  93. else
  94. {
  95. if (showWrong)
  96. {
  97. TryTimesDic.Remove(key);
  98. Debug.Log(string.Format($"post {key}", url, body));
  99. Alert.Show("连接服务器失败:\n请检查网络和服务器状态")
  100. .SetRightButton(true, "重试", (object data) =>
  101. {
  102. Post(url, body, callback, contentType, showWrong);
  103. });
  104. }
  105. }
  106. });
  107. }
  108. }
  109. public void ResultHandler(UnityWebRequest webRequest, Action<string> callback, string tag, Action retryFunc)
  110. {
  111. string paramCallback = null;
  112. if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  113. {
  114. Debug.LogWarning(webRequest.error + "\n" + webRequest.downloadHandler.text + "\nby " + tag);
  115. retryFunc?.Invoke();
  116. }
  117. else
  118. {
  119. //ET.Log.Debug("from server " + webRequest.downloadHandler.text + "\nby " + tag);
  120. paramCallback = webRequest.downloadHandler.text;
  121. //paramCallback = System.Text.Encoding.UTF8.GetString(webRequest.downloadHandler.data, 3, webRequest.downloadHandler.data.Length - 3);
  122. callback?.Invoke(paramCallback);
  123. }
  124. }
  125. }
  126. }