HttpTool.cs 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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 : MonoBehaviour
  15. {
  16. private static HttpTool _instacne = null;
  17. // private string baseUrl = "http://office.ghz-tech.com:9981";
  18. // private string baseUrl = "https://webapi-fat.shadowcreator.com/100022";
  19. // private string baseUrl = "http://192.168.140.123:8080/guideSystem";
  20. // private string baseUrl = "http://office.ghz-tech.com:3419/guideSystem";
  21. // private string baseUrl = "http://office.ghz-tech.com:3425/guideSystem";
  22. private string baseUrl = "https://office.ghz-tech.com:3424/api";
  23. // private string baseUrl = "http://office.ghz-tech.com:9903/guideSystem";
  24. // private string baseUrl = "http://ghz-tech.com:3419/guideSystem";
  25. // private string baseUrl = "http://192.168.43.142:8080";
  26. private string sKey = "zoo_visit_key";
  27. private string token = "";
  28. Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
  29. public static HttpTool Instance
  30. {
  31. get
  32. {
  33. if (_instacne == null)
  34. {
  35. Debug.LogError("Awake error");
  36. // HttpTool._instacne = gameObject.GetComponent<HttpTool>();
  37. }
  38. return _instacne;
  39. }
  40. }
  41. void Awake()
  42. {
  43. DontDestroyOnLoad(gameObject);
  44. HttpTool._instacne = gameObject.GetComponent<HttpTool>();
  45. //http header 的内容
  46. requestHeader.Add("Content-Type", "application/json");
  47. // requestHeader.Add("sKey", sKey);
  48. }
  49. public void Get(string methodName, Action<string> callback)
  50. {
  51. StartCoroutine(GetRequest(methodName, callback));
  52. }
  53. private IEnumerator GetRequest(string methodName, Action<string> callback)
  54. {
  55. // string url = Application.streamingAssetsPath + methodName;
  56. string url = baseUrl + methodName;
  57. Debug.Log(url);
  58. using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
  59. {
  60. webRequest.SetRequestHeader("authorization", token);
  61. //设置header
  62. foreach (var v in requestHeader)
  63. {
  64. webRequest.SetRequestHeader(v.Key, v.Value);
  65. }
  66. yield return webRequest.SendWebRequest();
  67. if (webRequest.isHttpError || webRequest.isNetworkError)
  68. {
  69. ErrorLogPanel.Instance.Show(" 请求后台数据出现错误 ");
  70. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  71. if (callback != null)
  72. {
  73. callback(null);
  74. }
  75. }
  76. else
  77. {
  78. if (callback != null)
  79. {
  80. callback(webRequest.downloadHandler.text);
  81. }
  82. }
  83. }
  84. }
  85. //public void Post( string methodName, string jsonString,Action<String> callback)
  86. //{
  87. // StartCoroutine(Post( methodName, jsonString, callback));
  88. //}
  89. //private IEnumerator PostRequest(string methodName, string jsonString, Action<String> callback)
  90. //{
  91. //}
  92. //jsonString 为json字符串,post提交的数据包为json
  93. // Loging 成功获取token 失败返回重新登录
  94. // 选择场景 根据选择的场景,获取当前场景的所有素材信息
  95. // 第一次下载 如果有素材之前没有下载过的,在这里需要进行下载/更新
  96. /// <summary>
  97. /// 文字
  98. /// </summary>
  99. /// <param name="methodName"></param>
  100. /// <param name="jsonString"></param>
  101. /// <param name="callback"></param>
  102. public void PostTxt(DownLoadMaterial value, string methodName, string jsonString, Action<DownLoadMaterial, object> callback)
  103. {
  104. StartCoroutine(PostRequestTxt(value, methodName, jsonString, callback));
  105. }
  106. private IEnumerator PostRequestTxt(DownLoadMaterial value, string methodName, string jsonString, Action<DownLoadMaterial, object> callback)
  107. {
  108. string url = baseUrl + methodName;
  109. Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
  110. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  111. {
  112. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  113. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  114. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath);
  115. foreach (var v in requestHeader)
  116. {
  117. webRequest.SetRequestHeader(v.Key, v.Value);
  118. }
  119. yield return webRequest.SendWebRequest();
  120. if (webRequest.isHttpError || webRequest.isNetworkError)
  121. {
  122. ErrorLogPanel.Instance.Show(" 请求后台数据出现错误 ");
  123. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  124. if (callback != null)
  125. {
  126. callback(value, null);
  127. }
  128. }
  129. else
  130. {
  131. if (callback != null)
  132. {
  133. File.WriteAllBytes(Application.persistentDataPath + "/Text/" + methodName, webRequest.downloadHandler.data);
  134. callback(value, webRequest.downloadHandler.text);
  135. }
  136. }
  137. }
  138. }
  139. #region Old
  140. //public void Post(string methodName, string jsonString,string function, Action<string ,string> callback)
  141. //{
  142. // StartCoroutine(PostRequest(methodName, jsonString, function, callback));
  143. //}
  144. //private IEnumerator PostRequest(string methodName, string jsonString, string function, Action<string ,string> callback)
  145. //{
  146. // string url = baseUrl + methodName;
  147. // Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
  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. // foreach (var v in requestHeader)
  154. // {
  155. // webRequest.SetRequestHeader(v.Key, v.Value);
  156. // }
  157. // yield return webRequest.SendWebRequest();
  158. // if (webRequest.isHttpError || webRequest.isNetworkError)
  159. // {
  160. // Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  161. // if (callback != null)
  162. // {
  163. // callback(function,null);
  164. // }
  165. // }
  166. // else
  167. // {
  168. // if (callback != null)
  169. // {
  170. // callback(function,webRequest.downloadHandler.text);
  171. // }
  172. // }
  173. // }
  174. //}
  175. #endregion
  176. /// <summary>
  177. /// 图片
  178. /// </summary>
  179. /// <param name="methodName"></param>
  180. /// <param name="jsonString"></param>
  181. /// <param name="callback"></param>
  182. public void PostImage(DownLoadMaterial value, string methodName, string jsonString, Action<DownLoadMaterial, object> callback)
  183. {
  184. StartCoroutine(PostRequestImage(value, methodName, jsonString, callback));
  185. }
  186. private IEnumerator PostRequestImage(DownLoadMaterial value, string methodName, string jsonString, Action<DownLoadMaterial, object> callback)
  187. {
  188. string url = baseUrl + methodName;
  189. Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
  190. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  191. {
  192. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  193. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  194. DownloadHandlerTexture texture = new DownloadHandlerTexture(true);
  195. webRequest.downloadHandler = texture;
  196. // Debug.Log(value.mObj.localSavePath);
  197. // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath);
  198. // Debug.Log(value.mObj.localSavePath);
  199. webRequest.SetRequestHeader("authorization", token);
  200. foreach (var v in requestHeader)
  201. {
  202. webRequest.SetRequestHeader(v.Key, v.Value);
  203. }
  204. yield return webRequest.SendWebRequest();
  205. if (webRequest.isHttpError || webRequest.isNetworkError)
  206. {
  207. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  208. if (callback != null)
  209. {
  210. callback(value, null);
  211. }
  212. ErrorLogPanel.Instance.Show(" 下载图片出现错误 " + jsonString);
  213. }
  214. else
  215. {
  216. if (callback != null)
  217. {
  218. try
  219. {
  220. string path = Application.persistentDataPath + "/Image";
  221. if (!Directory.Exists(path))
  222. {
  223. Directory.CreateDirectory(path);
  224. }
  225. byte[] data = new byte[webRequest.downloadHandler.data.Length];
  226. data = webRequest.downloadHandler.data;
  227. File.WriteAllBytes(value.mObj.localSavePath, data);
  228. }
  229. catch (Exception e)
  230. {
  231. ErrorLogPanel.Instance.Show(" 图片缓存出现错误 " + jsonString);
  232. }
  233. try
  234. {
  235. Texture2D texture2 = texture.texture;
  236. Sprite createSprite = Sprite.Create(texture2, new Rect(0, 0, texture2.width, texture2.height), Vector2.zero);
  237. // Debug.Log(webRequest.downloadHandler.data.Length + " "+ texture2.width);
  238. callback(value, createSprite);
  239. Texture2D texture3 = texture.texture;
  240. }
  241. catch (Exception e)
  242. {
  243. ErrorLogPanel.Instance.Show(" 图片生成出现错误 " + jsonString);
  244. }
  245. ////转为字节数组
  246. // File.WriteAllBytes(value.mObj.localSavePath, webRequest.downloadHandler.data);
  247. }
  248. }
  249. }
  250. }
  251. /// <summary>
  252. /// 视频
  253. /// </summary>
  254. /// <param name="methodName"></param>
  255. /// <param name="jsonString"></param>
  256. /// <param name="savePath"></param>
  257. public void PostVideo(DownLoadMaterial value, string methodName, string jsonString, Action<DownLoadMaterial, object> callback)
  258. {
  259. StartCoroutine(PostRequestVideo(value, methodName, jsonString, callback));
  260. }
  261. private IEnumerator PostRequestVideo(DownLoadMaterial value, string methodName, string jsonString, Action<DownLoadMaterial, object> callback)
  262. {
  263. string url = baseUrl + methodName;
  264. Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
  265. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  266. {
  267. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  268. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  269. // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerAudioClip(value.mObj.localSavePath, AudioType.ACC);
  270. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath);
  271. webRequest.SetRequestHeader("authorization", token);
  272. foreach (var v in requestHeader)
  273. {
  274. webRequest.SetRequestHeader(v.Key, v.Value);
  275. }
  276. yield return webRequest.SendWebRequest();
  277. if (webRequest.isHttpError || webRequest.isNetworkError)
  278. {
  279. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  280. callback(value, null);
  281. ErrorLogPanel.Instance.Show(" 下载视频出现错误 " + jsonString);
  282. }
  283. else
  284. {
  285. callback(value, value.mObj.localSavePath);
  286. // Debug.Log("文件下载成功 :" + value.mObj.DownloadPath +" "+ webRequest.downloadHandler.data.Length);
  287. //Debug.Log(webRequest.downloadHandler.data.Length);
  288. try
  289. {
  290. //string path = Application.persistentDataPath + "/Video";
  291. //if (!Directory.Exists(path))
  292. //{
  293. // Directory.CreateDirectory(path);
  294. //}
  295. //path = Application.persistentDataPath + "/StreamingAssets/Vuforia";
  296. //if (!Directory.Exists(path))
  297. //{
  298. // Directory.CreateDirectory(path);
  299. //}
  300. //File.WriteAllBytes(value.mObj.localSavePath, webRequest.downloadHandler.data);
  301. }
  302. catch (Exception)
  303. {
  304. ErrorLogPanel.Instance.Show(" 视频缓存出现错误 " + jsonString);
  305. }
  306. }
  307. }
  308. }
  309. /// <summary>
  310. /// Bundle 文件
  311. /// </summary>
  312. /// <param name="methodName"></param>
  313. /// <param name="jsonString"></param>
  314. /// <param name="savePath"></param>
  315. public void PostBundle(DownLoadMaterial mObj, string methodName, long time, string jsonString, Action<DownLoadMaterial, object> CallBack)
  316. {
  317. StartCoroutine(PostRequestBundle(mObj, methodName, time, jsonString, CallBack));
  318. }
  319. private IEnumerator PostRequestBundle(DownLoadMaterial mObj, string methodName, long time, string jsonString, Action<DownLoadMaterial, object> CallBack)
  320. {
  321. string url = baseUrl + methodName;
  322. //using (UnityWebRequest webRequestAB = UnityWebRequestAssetBundle.GetAssetBundle(url, (uint)time, 1))
  323. //{
  324. // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  325. // webRequestAB.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  326. // // webRequestAB.downloadHandler = (DownloadHandler)new DownloadHandlerFile(mObj.mObj.localSavePath);
  327. // yield return webRequestAB.SendWebRequest();
  328. // if (webRequestAB.isHttpError || webRequestAB.isNetworkError)
  329. // {
  330. // Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.data.Length);
  331. // }
  332. // else
  333. // {
  334. // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB);
  335. // GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
  336. // List<GameObject> listObjs = new List<GameObject>();
  337. // listObjs.Add(obj);
  338. // object value = listObjs;
  339. // CallBack(mObj, value);
  340. // }
  341. //}
  342. Dictionary<string, string> headers = new Dictionary<string, string>();
  343. headers["Content-Type"] = "application/json";
  344. headers["authorization"] = token;
  345. // webRequest.SetRequestHeader("authorization", token);
  346. //将文本转为byte数组
  347. byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes(jsonString);
  348. Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
  349. //向HTTP服务器提交Post数据
  350. WWW www = new WWW(url, bs, headers);
  351. //等待服务器的响应
  352. yield return www;
  353. if (www.error != null)
  354. {
  355. //获取服务器的错误信息
  356. Debug.LogError(www.error);
  357. ErrorLogPanel.Instance.Show(" 下载模型出现错误 " + jsonString);
  358. yield return null;
  359. }
  360. else
  361. {
  362. try
  363. {
  364. byte[] data = new byte[www.bytes.Length];
  365. data = www.bytes;
  366. Debug.Log(data.Length);
  367. string path = Application.persistentDataPath + "/Model";
  368. if (!Directory.Exists(path))
  369. {
  370. Directory.CreateDirectory(path);
  371. }
  372. File.WriteAllBytes(mObj.mObj.localSavePath, data);
  373. }
  374. catch (Exception e)
  375. {
  376. Debug.Log(e.Message.ToString());
  377. //ErrorLogPanel.Instance.Show(" 模型缓存出现错误 " + jsonString);
  378. }
  379. try
  380. {
  381. Debug.Log(www.bytes.Length);
  382. var ab = www.assetBundle;
  383. GameObject obj = ab.LoadAsset<GameObject>(ab.GetAllAssetNames()[0]);
  384. List<GameObject> listObjs = new List<GameObject>();
  385. listObjs.Add(obj);
  386. object value = listObjs;
  387. CallBack(mObj, value);
  388. }
  389. catch (Exception)
  390. {
  391. ErrorLogPanel.Instance.Show(" 模型生成出现错误 " + jsonString);
  392. }
  393. }
  394. //using (UnityWebRequest webRequestAB = new UnityWebRequest(url, "POST"))
  395. //{
  396. // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  397. // webRequestAB.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  398. // webRequestAB.downloadHandler = (DownloadHandler)new DownloadHandlerFile(mObj.mObj.localSavePath);
  399. // yield return webRequestAB.SendWebRequest();
  400. // if (webRequestAB.isHttpError || webRequestAB.isNetworkError)
  401. // {
  402. // Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.data.Length);
  403. // }
  404. // else
  405. // {
  406. // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB);
  407. // GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
  408. // List<GameObject> listObjs = new List<GameObject>();
  409. // listObjs.Add(obj);
  410. // object value = listObjs;
  411. // CallBack(mObj, value);
  412. // }
  413. //}
  414. }
  415. public void PostCDNImage(DownLoadMaterial value, string jsonString,Action<DownLoadMaterial,object> callback)
  416. {
  417. StartCoroutine(PostCDNRequestImage(value, jsonString, callback));
  418. }
  419. private IEnumerator PostCDNRequestImage(DownLoadMaterial value, string jsonString, Action<DownLoadMaterial, object> callback)
  420. {
  421. string url = value.mObj.cdnUrl;
  422. Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
  423. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  424. {
  425. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  426. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  427. DownloadHandlerTexture texture = new DownloadHandlerTexture(true);
  428. webRequest.downloadHandler = texture;
  429. // Debug.Log(value.mObj.localSavePath);
  430. // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath);
  431. // Debug.Log(value.mObj.localSavePath);
  432. webRequest.SetRequestHeader("authorization", token);
  433. foreach (var v in requestHeader)
  434. {
  435. webRequest.SetRequestHeader(v.Key, v.Value);
  436. }
  437. yield return webRequest.SendWebRequest();
  438. if (webRequest.isHttpError || webRequest.isNetworkError)
  439. {
  440. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  441. if (callback != null)
  442. {
  443. callback(value, null);
  444. }
  445. ErrorLogPanel.Instance.Show(" 下载图片出现错误 " + jsonString);
  446. }
  447. else
  448. {
  449. if (callback != null)
  450. {
  451. try
  452. {
  453. string path = Application.persistentDataPath + "/Image";
  454. if (!Directory.Exists(path))
  455. {
  456. Directory.CreateDirectory(path);
  457. }
  458. byte[] data = new byte[webRequest.downloadHandler.data.Length];
  459. data = webRequest.downloadHandler.data;
  460. File.WriteAllBytes(value.mObj.localSavePath, data);
  461. }
  462. catch (Exception e)
  463. {
  464. ErrorLogPanel.Instance.Show(" 图片缓存出现错误 " + jsonString);
  465. }
  466. try
  467. {
  468. Texture2D texture2 = texture.texture;
  469. Sprite createSprite = Sprite.Create(texture2, new Rect(0, 0, texture2.width, texture2.height), Vector2.zero);
  470. // Debug.Log(webRequest.downloadHandler.data.Length + " "+ texture2.width);
  471. callback(value, createSprite);
  472. Texture2D texture3 = texture.texture;
  473. }
  474. catch (Exception e)
  475. {
  476. ErrorLogPanel.Instance.Show(" 图片生成出现错误 " + jsonString);
  477. }
  478. ////转为字节数组
  479. // File.WriteAllBytes(value.mObj.localSavePath, webRequest.downloadHandler.data);
  480. }
  481. }
  482. }
  483. }
  484. public void PostCDNVideo(DownLoadMaterial value, string jsonString, Action<DownLoadMaterial, object> callback)
  485. {
  486. StartCoroutine(PostCDNRequestVideo(value, jsonString, callback));
  487. }
  488. private IEnumerator PostCDNRequestVideo(DownLoadMaterial value, string jsonString, Action<DownLoadMaterial, object> callback)
  489. {
  490. string url = value.mObj.cdnUrl;
  491. Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
  492. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  493. {
  494. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  495. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  496. // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerAudioClip(value.mObj.localSavePath, AudioType.ACC);
  497. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath);
  498. webRequest.SetRequestHeader("authorization", token);
  499. foreach (var v in requestHeader)
  500. {
  501. webRequest.SetRequestHeader(v.Key, v.Value);
  502. }
  503. yield return webRequest.SendWebRequest();
  504. if (webRequest.isHttpError || webRequest.isNetworkError)
  505. {
  506. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  507. callback(value, null);
  508. ErrorLogPanel.Instance.Show(" 下载视频出现错误 " + jsonString);
  509. }
  510. else
  511. {
  512. callback(value, value.mObj.localSavePath);
  513. // Debug.Log("文件下载成功 :" + value.mObj.DownloadPath +" "+ webRequest.downloadHandler.data.Length);
  514. //Debug.Log(webRequest.downloadHandler.data.Length);
  515. try
  516. {
  517. //string path = Application.persistentDataPath + "/Video";
  518. //if (!Directory.Exists(path))
  519. //{
  520. // Directory.CreateDirectory(path);
  521. //}
  522. //path = Application.persistentDataPath + "/StreamingAssets/Vuforia";
  523. //if (!Directory.Exists(path))
  524. //{
  525. // Directory.CreateDirectory(path);
  526. //}
  527. //File.WriteAllBytes(value.mObj.localSavePath, webRequest.downloadHandler.data);
  528. }
  529. catch (Exception)
  530. {
  531. ErrorLogPanel.Instance.Show(" 视频缓存出现错误 " + jsonString);
  532. }
  533. }
  534. }
  535. }
  536. /// <summary>
  537. /// Bundle 文件
  538. /// </summary>
  539. /// <param name="methodName"></param>
  540. /// <param name="jsonString"></param>
  541. /// <param name="savePath"></param>
  542. public void PostCDNBundle(DownLoadMaterial mObj, long time, string jsonString, Action<DownLoadMaterial, object> CallBack)
  543. {
  544. StartCoroutine(PostCDNRequestBundle(mObj, time, jsonString, CallBack));
  545. }
  546. private IEnumerator PostCDNRequestBundle(DownLoadMaterial mObj, long time, string jsonString, Action<DownLoadMaterial, object> CallBack)
  547. {
  548. string url = mObj.mObj.cdnUrl;
  549. //using (UnityWebRequest webRequestAB = UnityWebRequestAssetBundle.GetAssetBundle(url, (uint)time, 1))
  550. //{
  551. // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  552. // webRequestAB.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  553. // // webRequestAB.downloadHandler = (DownloadHandler)new DownloadHandlerFile(mObj.mObj.localSavePath);
  554. // yield return webRequestAB.SendWebRequest();
  555. // if (webRequestAB.isHttpError || webRequestAB.isNetworkError)
  556. // {
  557. // Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.data.Length);
  558. // }
  559. // else
  560. // {
  561. // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB);
  562. // GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
  563. // List<GameObject> listObjs = new List<GameObject>();
  564. // listObjs.Add(obj);
  565. // object value = listObjs;
  566. // CallBack(mObj, value);
  567. // }
  568. //}
  569. Dictionary<string, string> headers = new Dictionary<string, string>();
  570. headers["Content-Type"] = "application/json";
  571. headers["authorization"] = token;
  572. // webRequest.SetRequestHeader("authorization", token);
  573. //将文本转为byte数组
  574. byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes(jsonString);
  575. Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
  576. //向HTTP服务器提交Post数据
  577. WWW www = new WWW(url, bs, headers);
  578. //等待服务器的响应
  579. yield return www;
  580. if (www.error != null)
  581. {
  582. //获取服务器的错误信息
  583. Debug.LogError(www.error);
  584. ErrorLogPanel.Instance.Show(" 下载模型出现错误 " + jsonString);
  585. yield return null;
  586. }
  587. else
  588. {
  589. try
  590. {
  591. byte[] data = new byte[www.bytes.Length];
  592. data = www.bytes;
  593. Debug.Log(data.Length);
  594. string path = Application.persistentDataPath + "/Model";
  595. if (!Directory.Exists(path))
  596. {
  597. Directory.CreateDirectory(path);
  598. }
  599. File.WriteAllBytes(mObj.mObj.localSavePath, data);
  600. }
  601. catch (Exception e)
  602. {
  603. Debug.Log(e.Message.ToString());
  604. //ErrorLogPanel.Instance.Show(" 模型缓存出现错误 " + jsonString);
  605. }
  606. try
  607. {
  608. Debug.Log(www.bytes.Length);
  609. var ab = www.assetBundle;
  610. GameObject obj = ab.LoadAsset<GameObject>(ab.GetAllAssetNames()[0]);
  611. List<GameObject> listObjs = new List<GameObject>();
  612. listObjs.Add(obj);
  613. object value = listObjs;
  614. CallBack(mObj, value);
  615. }
  616. catch (Exception)
  617. {
  618. ErrorLogPanel.Instance.Show(" 模型生成出现错误 " + jsonString);
  619. }
  620. }
  621. //using (UnityWebRequest webRequestAB = new UnityWebRequest(url, "POST"))
  622. //{
  623. // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  624. // webRequestAB.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  625. // webRequestAB.downloadHandler = (DownloadHandler)new DownloadHandlerFile(mObj.mObj.localSavePath);
  626. // yield return webRequestAB.SendWebRequest();
  627. // if (webRequestAB.isHttpError || webRequestAB.isNetworkError)
  628. // {
  629. // Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.data.Length);
  630. // }
  631. // else
  632. // {
  633. // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB);
  634. // GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
  635. // List<GameObject> listObjs = new List<GameObject>();
  636. // listObjs.Add(obj);
  637. // object value = listObjs;
  638. // CallBack(mObj, value);
  639. // }
  640. //}
  641. }
  642. public void PostTest(string Loadpath, string jsonString, Action<string> CallBack)
  643. {
  644. StartCoroutine(PostRequest(Loadpath, jsonString, CallBack));
  645. }
  646. //
  647. private IEnumerator PostRequest(string loadPath, string jsonString, Action<string> CallBack)
  648. {
  649. string url = baseUrl + loadPath;
  650. Debug.Log(url);
  651. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  652. {
  653. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  654. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  655. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  656. webRequest.SetRequestHeader("authorization", token);
  657. foreach (var v in requestHeader)
  658. {
  659. webRequest.SetRequestHeader(v.Key, v.Value);
  660. // Debug.Log(v.Value + " " + loadPath);
  661. }
  662. yield return webRequest.SendWebRequest();
  663. if (webRequest.isHttpError || webRequest.isNetworkError)
  664. {
  665. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + loadPath+" "+ jsonString);
  666. GameManager.Instance.text2.text = jsonString + " " + webRequest.error;
  667. }
  668. else
  669. {
  670. // Debug.Log(webRequest.downloadHandler.text);
  671. CallBack(webRequest.downloadHandler.text);
  672. }
  673. }
  674. }
  675. public void GetAllMaterials(string methodName, string jsonString, Action<String> CallBack)
  676. {
  677. StartCoroutine(GetRequest(methodName, jsonString, CallBack));
  678. }
  679. private IEnumerator GetRequest(string methodName, string jsonString, Action<String> CallBack)
  680. {
  681. string url = baseUrl + methodName;
  682. Debug.Log(url);
  683. token = jsonString;
  684. HeadAddToken(token);
  685. using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
  686. {
  687. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  688. webRequest.SetRequestHeader("authorization", jsonString);
  689. foreach (var v in requestHeader)
  690. {
  691. Debug.Log(v.Key + " " + v.Value);
  692. webRequest.SetRequestHeader(v.Key, v.Value);
  693. }
  694. yield return webRequest.SendWebRequest();
  695. if (webRequest.isHttpError || webRequest.isNetworkError)
  696. {
  697. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  698. }
  699. else
  700. {
  701. Debug.Log(webRequest.downloadHandler.text);
  702. CallBack(webRequest.downloadHandler.text);
  703. }
  704. }
  705. }
  706. /// <summary>
  707. /// 登录
  708. /// </summary>
  709. /// <param name="methodName"></param>
  710. /// <param name="jsonString"></param>
  711. /// <param name="CallBack"></param>
  712. public void PostLogin(string methodName, string jsonString, Action<string> CallBack)
  713. {
  714. StartCoroutine(PostRequestLogin(methodName, jsonString, CallBack));
  715. }
  716. private IEnumerator PostRequestLogin(string methodName, string jsonString, Action<string> CallBack)
  717. {
  718. string url = baseUrl + methodName;
  719. Debug.Log(jsonString);
  720. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  721. {
  722. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  723. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  724. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  725. foreach (var v in requestHeader)
  726. {
  727. webRequest.SetRequestHeader(v.Key, v.Value);
  728. }
  729. yield return webRequest.SendWebRequest();
  730. if (webRequest.isHttpError || webRequest.isNetworkError)
  731. {
  732. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  733. string error = webRequest.downloadHandler.text;
  734. JObject jObject = JObject.Parse(error);
  735. CallBack(jObject["message"].ToString());
  736. ErrorLogPanel.Instance.Show(" 请求后台数据出现错误 ");
  737. }
  738. else
  739. {
  740. Debug.Log(webRequest.downloadHandler.text);
  741. token = webRequest.downloadHandler.text;
  742. if (token.Contains("密码不正确,请重新输入") || token.Contains("用户未注册"))
  743. {
  744. CallBack(token);
  745. }
  746. else
  747. {
  748. JObject obj = JObject.Parse(token);
  749. Debug.Log(obj["data"]["token"].ToString());
  750. token = obj["data"]["token"].ToString();
  751. Debug.Log(token);
  752. CallBack(token);
  753. }
  754. }
  755. }
  756. }
  757. /// <summary>
  758. /// 获取本地Sprite
  759. /// </summary>
  760. /// <param name="mObj"></param>
  761. /// <param name="CallBack"></param>
  762. public void GetLocalSprite(DownLoadMaterial mObj, Action<DownLoadMaterial, object> CallBack)
  763. {
  764. // StartCoroutine(GetSpriteRequest(mObj, CallBack));
  765. try
  766. {
  767. var pathName = mObj.mObj.localSavePath;
  768. var bytes = ReadFile(pathName);
  769. int width = Screen.width;
  770. int height = Screen.height;
  771. var texture = new Texture2D(width, height);
  772. texture.LoadImage(bytes);
  773. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
  774. CallBack(mObj, sprite);
  775. }
  776. catch (Exception c)
  777. {
  778. ErrorLogPanel.Instance.Show(" 加载缓存图片出现错误 " + mObj.mObj.localSavePath);
  779. Debug.LogError(c.Message);
  780. }
  781. }
  782. private IEnumerator GetSpriteRequest(DownLoadMaterial mObj, Action<DownLoadMaterial, object> CallBack)
  783. {
  784. //WWW www = new WWW(mObj.mObj.localSavePath);
  785. //yield return www;
  786. //if (www.isDone && www.error == null)
  787. //{
  788. // Texture2D texture = www.texture;
  789. // Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
  790. // CallBack(mObj, sprite);
  791. //}
  792. //else
  793. //{
  794. // Debug.Log(mObj.mObj.localSavePath+" " + www.error);
  795. //}
  796. UnityWebRequest request = UnityWebRequestTexture.GetTexture(mObj.mObj.localSavePath);
  797. Debug.Log(mObj.mObj.localSavePath);
  798. yield return request.SendWebRequest();
  799. if (request.isNetworkError || request.isHttpError)
  800. {
  801. Debug.LogError(request.error);
  802. }
  803. else
  804. {
  805. Texture2D texture = DownloadHandlerTexture.GetContent(request);
  806. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
  807. CallBack(mObj, sprite);
  808. // LoadManager.Instance.DownLoadEnd(mObj, sprite);
  809. }
  810. }
  811. public byte[] ReadFile(string filePath)
  812. {
  813. var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  814. fs.Seek(0, SeekOrigin.Begin);
  815. var binary = new byte[fs.Length];
  816. fs.Read(binary, 0, binary.Length);
  817. fs.Close();
  818. return binary;
  819. }
  820. public void GetLocalModel(DownLoadMaterial mObj, Action<DownLoadMaterial, object> CallBack)
  821. {
  822. StartCoroutine(GetModelRequest(mObj, CallBack));
  823. }
  824. private IEnumerator GetModelRequest(DownLoadMaterial mObj, Action<DownLoadMaterial, object> CallBack)
  825. {
  826. //Debug.Log(" Load LocalModel");
  827. //UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(mObj.mObj.localSavePath);
  828. //yield return request.SendWebRequest();
  829. //if (request.isHttpError || request.isNetworkError)
  830. //{
  831. // Debug.LogError(request.error + "\n" + request.downloadHandler.text);
  832. // ErrorLogPanel.Instance.Show(" 加载缓存模型出现错误 " + mObj.mObj.localSavePath);
  833. //}
  834. //else
  835. //{
  836. // try
  837. // {
  838. // Debug.Log(" Load LocalModel2");
  839. // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
  840. // GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
  841. // List<GameObject> listObjs = new List<GameObject>();
  842. // listObjs.Add(obj);
  843. // LoadManager.Instance.DownLoadEnd(mObj, listObjs);
  844. // bundle.Unload(false);
  845. // object value = listObjs;
  846. // CallBack(mObj, value);
  847. // }
  848. // catch (Exception)
  849. // {
  850. // ErrorLogPanel.Instance.Show(" 生成缓存模型出现错误 " + mObj.mObj.localSavePath);
  851. // throw;
  852. // }
  853. //}
  854. AssetBundleCreateRequest request1 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(mObj.mObj.localSavePath));
  855. yield return request1;
  856. if (false)
  857. {
  858. }
  859. else
  860. {
  861. try
  862. {
  863. Debug.Log(" Load LocalModel2");
  864. //AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
  865. // AssetBundle bundle = request.downloadHandler.data;
  866. AssetBundle bundle = request1.assetBundle;
  867. GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
  868. List<GameObject> listObjs = new List<GameObject>();
  869. listObjs.Add(obj);
  870. LoadManager.Instance.DownLoadEnd(mObj, listObjs);
  871. bundle.Unload(false);
  872. object value = listObjs;
  873. CallBack(mObj, value);
  874. }
  875. catch (Exception)
  876. {
  877. ErrorLogPanel.Instance.Show(" 生成缓存模型出现错误 " + mObj.mObj.localSavePath);
  878. throw;
  879. }
  880. }
  881. }
  882. public void HeadAddToken(string token)
  883. {
  884. requestHeader.Add("x-token", token);
  885. }
  886. public string GetMd5Hash(string strToEncrypt)
  887. {
  888. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  889. byte[] bytes = Encoding.ASCII.GetBytes(strToEncrypt);
  890. byte[] encoded = md5.ComputeHash(bytes);
  891. StringBuilder sb = new StringBuilder();
  892. for (int i = 0; i < 10; i++)
  893. {
  894. sb.Append(encoded[i].ToString("x2"));
  895. }
  896. string password = sb.ToString();
  897. password = password.Substring(0, 10);
  898. return password;
  899. }
  900. }