HttpTool.cs 14 KB

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