HttpTool.cs 14 KB

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