HttpTool.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. // private string baseUrl = "http://office.ghz-tech.com:3425/guideSystem";
  17. //private string baseUrl = "http://office.ghz-tech.com:9904/api";
  18. private string baseUrl = "https://nav21.ghz-tech.com:8843/api/";
  19. private string UrlPointCloud = "https://pro.qulivr.com/mr-navigator/v1";
  20. private string token = "";
  21. Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
  22. void Awake()
  23. {
  24. //http header 的内容
  25. requestHeader.Add("Content-Type", "application/json");
  26. }
  27. public string Token
  28. {
  29. get { return token; }
  30. }
  31. public string BaseUrl
  32. {
  33. get { return baseUrl; }
  34. }
  35. public Dictionary<string, string> RequestHeader
  36. {
  37. get { return requestHeader; }
  38. }
  39. public void Get(string methodName, Action<string> callback)
  40. {
  41. StartCoroutine(GetRequest(methodName, callback));
  42. }
  43. private IEnumerator GetRequest(string methodName, Action<string> callback)
  44. {
  45. // string url = Application.streamingAssetsPath + methodName;
  46. string url = baseUrl + methodName;
  47. //Debug.Log("URL:"+url);
  48. using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
  49. {
  50. webRequest.SetRequestHeader("authorization", token);
  51. //设置header
  52. foreach (var v in requestHeader)
  53. {
  54. webRequest.SetRequestHeader(v.Key, v.Value);
  55. }
  56. yield return webRequest.SendWebRequest();
  57. if (webRequest.isHttpError || webRequest.isNetworkError)
  58. {
  59. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  60. if (callback != null)
  61. {
  62. callback(null);
  63. }
  64. }
  65. else
  66. {
  67. if (callback != null)
  68. {
  69. callback(webRequest.downloadHandler.text);
  70. }
  71. }
  72. }
  73. }
  74. public void PostTest(string methodName, string jsonString, Action<string> CallBack)
  75. {
  76. StartCoroutine(PostRequestTest(methodName, jsonString, CallBack));
  77. }
  78. private IEnumerator PostRequestTest(string methodName, string jsonString, Action<string> CallBack)
  79. {
  80. string url = baseUrl + methodName;
  81. Debug.Log(url + jsonString);
  82. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  83. {
  84. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  85. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  86. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  87. webRequest.SetRequestHeader("authorization", token);
  88. foreach (var v in requestHeader)
  89. {
  90. webRequest.SetRequestHeader(v.Key, v.Value);
  91. Debug.Log(v.Value + " " + methodName);
  92. }
  93. webRequest.timeout = 5;
  94. yield return webRequest.SendWebRequest();
  95. if (webRequest.isHttpError || webRequest.isNetworkError)
  96. {
  97. CallBack?.Invoke("UnityWebRequest Error");
  98. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
  99. //GameManager.Instance.text2.text = webRequest.error;
  100. }
  101. else
  102. {
  103. Debug.Log(webRequest.downloadHandler.text);
  104. CallBack(webRequest.downloadHandler.text);
  105. }
  106. }
  107. }
  108. public void PostSnInfo(string methodName, string jsonString, int index, Action<int, string> CallBack)
  109. {
  110. StartCoroutine(PostnInfoRequest(methodName, jsonString, index, CallBack));
  111. }
  112. public IEnumerator PostnInfoRequest(string methodName, string jsonString, int index, Action<int, string> CallBack)
  113. {
  114. string url = baseUrl + methodName;
  115. Debug.Log("URL:" + url);
  116. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  117. {
  118. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  119. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  120. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  121. webRequest.SetRequestHeader("authorization", token);
  122. foreach (var v in requestHeader)
  123. {
  124. webRequest.SetRequestHeader(v.Key, v.Value);
  125. //Debug.Log(v.Value + " " + methodName);
  126. }
  127. yield return webRequest.SendWebRequest();
  128. if (webRequest.isHttpError || webRequest.isNetworkError)
  129. {
  130. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
  131. //GameManager.Instance.text2.text = webRequest.error;
  132. }
  133. else
  134. {
  135. Debug.Log(webRequest.downloadHandler.text);
  136. CallBack(index, webRequest.downloadHandler.text);
  137. }
  138. }
  139. }
  140. public IEnumerator PostRequestSpoitDetail(string methodName, string jsonString, Action<string> CallBack)
  141. {
  142. string url = baseUrl + methodName;
  143. Debug.Log(url);
  144. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  145. {
  146. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  147. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  148. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  149. webRequest.SetRequestHeader("authorization", token);
  150. foreach (var v in requestHeader)
  151. {
  152. webRequest.SetRequestHeader(v.Key, v.Value);
  153. //Debug.Log(v.Value + " " + methodName);
  154. }
  155. yield return webRequest.SendWebRequest();
  156. if (webRequest.isHttpError || webRequest.isNetworkError)
  157. {
  158. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
  159. //GameManager.Instance.text2.text = webRequest.error;
  160. }
  161. else
  162. {
  163. //Debug.Log(webRequest.downloadHandler.text);
  164. CallBack(webRequest.downloadHandler.text);
  165. }
  166. }
  167. }
  168. public void Post(string methodName, string jsonString, Action<string> CallBack)
  169. {
  170. StartCoroutine(PostRequest(methodName, jsonString, CallBack));
  171. }
  172. //
  173. private IEnumerator PostRequest(string methodName, string jsonString, Action<string> CallBack)
  174. {
  175. string url = baseUrl + methodName;
  176. //Debug.Log(url);
  177. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  178. {
  179. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  180. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  181. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  182. webRequest.SetRequestHeader("authorization", token);
  183. foreach (var v in requestHeader)
  184. {
  185. webRequest.SetRequestHeader(v.Key, v.Value);
  186. //Debug.Log(v.Value + " " + methodName);
  187. }
  188. yield return webRequest.SendWebRequest();
  189. if (webRequest.isHttpError || webRequest.isNetworkError)
  190. {
  191. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
  192. //GameManager.Instance.text2.text = webRequest.error;
  193. }
  194. else
  195. {
  196. //Debug.Log(webRequest.downloadHandler.text);
  197. CallBack(webRequest.downloadHandler.text);
  198. }
  199. }
  200. }
  201. public void GetAllMaterials(string methodName, string jsonString, Action<String> CallBack)
  202. {
  203. StartCoroutine(GetRequest(methodName, jsonString, CallBack));
  204. }
  205. private IEnumerator GetRequest(string methodName, string jsonString, Action<String> CallBack)
  206. {
  207. string url = baseUrl + methodName;
  208. //Debug.Log("URL:"+url);
  209. token = jsonString;
  210. HeadAddToken(token);
  211. using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
  212. {
  213. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  214. webRequest.SetRequestHeader("authorization", jsonString);
  215. foreach (var v in requestHeader)
  216. {
  217. //Debug.Log(v.Key + " " + v.Value);
  218. webRequest.SetRequestHeader(v.Key, v.Value);
  219. }
  220. yield return webRequest.SendWebRequest();
  221. if (webRequest.isHttpError || webRequest.isNetworkError)
  222. {
  223. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  224. }
  225. else
  226. {
  227. //Debug.Log(webRequest.downloadHandler.text);
  228. CallBack(webRequest.downloadHandler.text);
  229. }
  230. }
  231. }
  232. /// <summary>
  233. /// 登录
  234. /// </summary>
  235. /// <param name="methodName"></param>
  236. /// <param name="jsonString"></param>
  237. /// <param name="CallBack"></param>
  238. public void PostLogin(string methodName, string jsonString, Action<string> CallBack)
  239. {
  240. StartCoroutine(PostRequestLogin(methodName, jsonString, CallBack));
  241. }
  242. private IEnumerator PostRequestLogin(string methodName, string jsonString, Action<string> CallBack)
  243. {
  244. string url = baseUrl + methodName;
  245. Debug.Log("URL:" + url);
  246. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  247. {
  248. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  249. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  250. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  251. foreach (var v in requestHeader)
  252. {
  253. webRequest.SetRequestHeader(v.Key, v.Value);
  254. }
  255. yield return webRequest.SendWebRequest();
  256. if (webRequest.isHttpError || webRequest.isNetworkError)
  257. {
  258. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  259. string error = webRequest.downloadHandler.text;
  260. JObject jObject = JObject.Parse(error);
  261. CallBack(jObject["message"].ToString());
  262. }
  263. else
  264. {
  265. //Debug.Log(webRequest.downloadHandler.text);
  266. var mes = webRequest.downloadHandler.text;
  267. JObject obj = JObject.Parse(mes);
  268. if (obj["code"].ToString() == "200")
  269. {
  270. //Debug.Log(obj["data"]["token"].ToString());
  271. token = obj["data"]["token"].ToString();
  272. //Debug.Log(mes);
  273. }
  274. CallBack(mes);
  275. }
  276. }
  277. }
  278. ///// <summary>
  279. ///// 获取本地Sprite
  280. ///// </summary>
  281. ///// <param name="mObj"></param>
  282. ///// <param name="CallBack"></param>
  283. //public void GetLocalSprite(DownLoadMaterial mObj, Action<DownLoadMaterial, object> CallBack)
  284. //{
  285. // StartCoroutine(GetSpriteRequest(mObj, CallBack));
  286. //}
  287. //private IEnumerator GetSpriteRequest(DownLoadMaterial mObj, Action<DownLoadMaterial, object> CallBack)
  288. //{
  289. // UnityWebRequest request = UnityWebRequestTexture.GetTexture(mObj.mObj.localSavePath);
  290. // yield return request.SendWebRequest();
  291. // if (request.isNetworkError || request.isHttpError)
  292. // {
  293. // Debug.LogError(request.error);
  294. // }
  295. // else
  296. // {
  297. // Texture2D texture = DownloadHandlerTexture.GetContent(request);
  298. // Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
  299. // CallBack(mObj, sprite);
  300. // // LoadManager.Instance.DownLoadEnd(mObj, sprite);
  301. // }
  302. //}
  303. public void HeadAddToken(string token)
  304. {
  305. requestHeader.Add("x-token", token);
  306. }
  307. public string GetMd5Hash(string strToEncrypt)
  308. {
  309. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  310. byte[] bytes = Encoding.ASCII.GetBytes(strToEncrypt);
  311. byte[] encoded = md5.ComputeHash(bytes);
  312. StringBuilder sb = new StringBuilder();
  313. for (int i = 0; i < 10; i++)
  314. {
  315. sb.Append(encoded[i].ToString("x2"));
  316. }
  317. string password = sb.ToString();
  318. password = password.Substring(0, 10);
  319. return password;
  320. }
  321. }