HttpTool.cs 3.9 KB

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