HttpTool.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using UnityEngine.Networking;
  6. using System.Text;
  7. /// <summary>
  8. /// Http Request SDK
  9. /// </summary>
  10. public class HttpTool : MonoBehaviour
  11. {
  12. private static HttpTool _instacne = null;
  13. private string baseUrl = "http://office.ghz-tech.com:9981";
  14. private string sKey = "zoo_visit_key";
  15. Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
  16. public static HttpTool Instance
  17. {
  18. get
  19. {
  20. if (_instacne == null)
  21. {
  22. Debug.LogError("Awake error");
  23. }
  24. return _instacne;
  25. }
  26. }
  27. void Awake()
  28. {
  29. DontDestroyOnLoad(gameObject);
  30. HttpTool._instacne = gameObject.GetComponent<HttpTool>();
  31. //http header 的内容
  32. requestHeader.Add("Content-Type", "application/json");
  33. // requestHeader.Add("sKey", sKey);
  34. }
  35. public void Get(string methodName, Action<string> callback)
  36. {
  37. StartCoroutine(GetRequest(methodName, callback));
  38. }
  39. private IEnumerator GetRequest(string methodName, Action<string> callback)
  40. {
  41. string url = baseUrl + methodName;
  42. using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
  43. {
  44. //设置header
  45. foreach (var v in requestHeader)
  46. {
  47. webRequest.SetRequestHeader(v.Key, v.Value);
  48. }
  49. yield return webRequest.SendWebRequest();
  50. if (webRequest.isHttpError || webRequest.isNetworkError)
  51. {
  52. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  53. if (callback != null)
  54. {
  55. callback(null);
  56. }
  57. }
  58. else
  59. {
  60. if (callback != null)
  61. {
  62. callback(webRequest.downloadHandler.text);
  63. }
  64. }
  65. }
  66. }
  67. //jsonString 为json字符串,post提交的数据包为json
  68. public void Post(string methodName, string jsonString, Action<string> callback)
  69. {
  70. StartCoroutine(PostRequest(methodName, jsonString, callback));
  71. }
  72. private IEnumerator PostRequest(string methodName, string jsonString, Action<string> callback)
  73. {
  74. string url = baseUrl;
  75. // Debug.Log(string.Format("url:{0} postData:{1}",url,jsonString));
  76. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  77. {
  78. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  79. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  80. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  81. foreach (var v in requestHeader)
  82. {
  83. webRequest.SetRequestHeader(v.Key, v.Value);
  84. }
  85. yield return webRequest.SendWebRequest();
  86. if (webRequest.isHttpError || webRequest.isNetworkError)
  87. {
  88. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  89. if (callback != null)
  90. {
  91. callback(null);
  92. }
  93. }
  94. else
  95. {
  96. if (callback != null)
  97. {
  98. callback(webRequest.downloadHandler.text);
  99. }
  100. }
  101. }
  102. }
  103. }