HttpTool.cs 39 KB

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