HttpTool.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using UnityEngine.Networking;
  6. using System.Text;
  7. using System.Security.Cryptography;
  8. using System.Net;
  9. using System.IO;
  10. using Newtonsoft.Json.Linq;
  11. /// <summary>
  12. /// Http Request SDK
  13. /// </summary>
  14. public class HttpTool : MonoSingleton<HttpTool>
  15. {
  16. Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
  17. void Awake()
  18. {
  19. //http header 的内容
  20. requestHeader.Add("Content-Type", "application/json");
  21. }
  22. // public string Token
  23. // {
  24. // get { return token; }
  25. // }
  26. public Dictionary<string, string> RequestHeader
  27. {
  28. get { return requestHeader; }
  29. }
  30. public void Get(string methodName, Action<string> callback)
  31. {
  32. StartCoroutine(SendHttp(methodName,"", callback,false, false));
  33. }
  34. public void GetPoint(string methodName, Action<string> callback,bool isCloud=false)
  35. {
  36. StartCoroutine(SendHttp(methodName,"", callback,false, isCloud));
  37. }
  38. public void GetPointFile(string methodName, string projectId, string fileType, Action<string> callback)
  39. {
  40. string url =HttpAction.UrlPointCloud + methodName;
  41. // 创建URL
  42. UriBuilder uriBuilder = new UriBuilder(url);
  43. // 添加查询参数
  44. uriBuilder.Query = "projectId=" + projectId + "&fileType=" + fileType;
  45. StartCoroutine(SendHttp(uriBuilder.Uri.ToString(),"", callback,false));
  46. }
  47. public void PostTest(string methodName, string jsonString, Action<string> CallBack)
  48. {
  49. StartCoroutine(SendHttp(methodName, jsonString, CallBack));
  50. }
  51. public void Post(string methodName, string jsonString, Action<string> CallBack)
  52. {
  53. StartCoroutine(SendHttp(methodName, jsonString, CallBack));
  54. }
  55. public void GetAllMaterials(string methodName, string jsonString, Action<String> CallBack)
  56. {
  57. StartCoroutine(SendHttp(methodName, "", CallBack,false));
  58. }
  59. /// <summary>
  60. /// 登录
  61. /// </summary>
  62. /// <param name="methodName"></param>
  63. /// <param name="jsonString"></param>
  64. /// <param name="CallBack"></param>
  65. public void PostLogin(string methodName, string jsonString, Action<string> CallBack)
  66. {
  67. StartCoroutine(SendHttp(methodName, jsonString, CallBack));
  68. }
  69. public void initHead()
  70. {
  71. requestHeader.Clear();
  72. if (UserInfo.Instance.Token!="" && UserInfo.Instance.Token!=null)
  73. {
  74. requestHeader.Add("x-token", UserInfo.Instance.Token);
  75. requestHeader.Add("authorization", UserInfo.Instance.Token);
  76. }
  77. requestHeader.Add("Content-Type", "application/json");
  78. }
  79. public IEnumerator SendHttp(string methodName, string jsonString, Action<string> CallBack,bool isPost=true,bool isCloud=false)
  80. {
  81. Debug.Log("Start Queue SendHttp " + methodName);
  82. string url;
  83. if (methodName.Contains("http"))
  84. {
  85. url =methodName;
  86. }
  87. else
  88. {
  89. if(isCloud)
  90. {
  91. url = HttpAction.UrlPointCloud + methodName;
  92. }else
  93. {
  94. if(UserInfo.Instance.is20)
  95. {
  96. url = HttpAction.baseurl20 + methodName;
  97. }
  98. else
  99. {
  100. url = HttpAction.baseurl + methodName;
  101. }
  102. }
  103. }
  104. Debug.Log("URL:" + url+" isPost "+ isPost);
  105. UnityWebRequest webRequest;
  106. if (!isPost)
  107. {
  108. webRequest = UnityWebRequest.Get(url);
  109. }
  110. else
  111. {
  112. webRequest = new UnityWebRequest(url, "POST");
  113. }
  114. initHead();
  115. using (webRequest)
  116. {
  117. if (!isPost)
  118. {
  119. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  120. foreach (var v in requestHeader)
  121. {
  122. webRequest.SetRequestHeader(v.Key, v.Value);
  123. }
  124. }
  125. else
  126. {
  127. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  128. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  129. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  130. }
  131. foreach (var v in requestHeader)
  132. {
  133. webRequest.SetRequestHeader(v.Key, v.Value);
  134. }
  135. yield return webRequest.SendWebRequest();
  136. Debug.Log("CallBack==>"+ webRequest.downloadHandler.text);
  137. if (webRequest.isHttpError || webRequest.isNetworkError)
  138. {
  139. Debug.LogError(url + "\n" + webRequest.error + "\n" + webRequest.downloadHandler.text);
  140. string error = webRequest.downloadHandler.text;
  141. JObject jObject = JObject.Parse(error);
  142. CallBack(jObject["message"].ToString());
  143. }
  144. else
  145. {
  146. //Debug.Log(webRequest.downloadHandler.text);
  147. var mes = webRequest.downloadHandler.text;
  148. CallBack(mes);
  149. }
  150. }
  151. }
  152. public void HeadAddToken(string token)
  153. {
  154. requestHeader.Add("x-token", token);
  155. }
  156. public string GetMd5Hash(string strToEncrypt)
  157. {
  158. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  159. byte[] bytes = Encoding.ASCII.GetBytes(strToEncrypt);
  160. byte[] encoded = md5.ComputeHash(bytes);
  161. StringBuilder sb = new StringBuilder();
  162. for (int i = 0; i < 10; i++)
  163. {
  164. sb.Append(encoded[i].ToString("x2"));
  165. }
  166. string password = sb.ToString();
  167. password = password.Substring(0, 10);
  168. return password;
  169. }
  170. }