HttpTool.cs 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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. // Debug.Log(value.mObj.localSavePath);
  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. path = Application.persistentDataPath + "/ImageTracked";
  226. if (!Directory.Exists(path))
  227. {
  228. Directory.CreateDirectory(path);
  229. }
  230. byte[] data = new byte[webRequest.downloadHandler.data.Length];
  231. data = webRequest.downloadHandler.data;
  232. Debug.Log(value.mObj.localSavePath);
  233. File.WriteAllBytes(value.mObj.localSavePath, data);
  234. }
  235. catch (Exception e)
  236. {
  237. ErrorLogPanel.Instance.Show(" 图片缓存出现错误 " + jsonString);
  238. Debug.LogError(e.Message);
  239. }
  240. Texture2D texture2 = texture.texture;
  241. Sprite createSprite = Sprite.Create(texture2, new Rect(0, 0, texture2.width, texture2.height), Vector2.zero);
  242. Debug.Log(webRequest.downloadHandler.data.Length + " " + texture2.width);
  243. callback(value, createSprite);
  244. Texture2D texture3 = texture.texture;
  245. try
  246. {
  247. }
  248. catch (Exception e)
  249. {
  250. ErrorLogPanel.Instance.Show(" 图片生成出现错误 " + jsonString);
  251. Debug.LogError(e.Message);
  252. }
  253. ////转为字节数组
  254. // File.WriteAllBytes(value.mObj.localSavePath, webRequest.downloadHandler.data);
  255. }
  256. }
  257. }
  258. }
  259. /// <summary>
  260. /// 视频
  261. /// </summary>
  262. /// <param name="methodName"></param>
  263. /// <param name="jsonString"></param>
  264. /// <param name="savePath"></param>
  265. public void PostVideo(DownLoadMaterial value, string methodName, string jsonString, Action<DownLoadMaterial, object> callback)
  266. {
  267. StartCoroutine(PostRequestVideo(value, methodName, jsonString, callback));
  268. }
  269. private IEnumerator PostRequestVideo(DownLoadMaterial value, string methodName, string jsonString, Action<DownLoadMaterial, object> callback)
  270. {
  271. string url = baseUrl + methodName;
  272. Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
  273. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  274. {
  275. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  276. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  277. // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerAudioClip(value.mObj.localSavePath, AudioType.ACC);
  278. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath);
  279. webRequest.SetRequestHeader("authorization", token);
  280. foreach (var v in requestHeader)
  281. {
  282. webRequest.SetRequestHeader(v.Key, v.Value);
  283. }
  284. yield return webRequest.SendWebRequest();
  285. if (webRequest.isHttpError || webRequest.isNetworkError)
  286. {
  287. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  288. callback(value, null);
  289. ErrorLogPanel.Instance.Show(" 下载视频出现错误 " + jsonString);
  290. }
  291. else
  292. {
  293. callback(value, value.mObj.localSavePath);
  294. // Debug.Log("文件下载成功 :" + value.mObj.DownloadPath +" "+ webRequest.downloadHandler.data.Length);
  295. //Debug.Log(webRequest.downloadHandler.data.Length);
  296. try
  297. {
  298. //string path = Application.persistentDataPath + "/Video";
  299. //if (!Directory.Exists(path))
  300. //{
  301. // Directory.CreateDirectory(path);
  302. //}
  303. //path = Application.persistentDataPath + "/StreamingAssets/Vuforia";
  304. //if (!Directory.Exists(path))
  305. //{
  306. // Directory.CreateDirectory(path);
  307. //}
  308. //File.WriteAllBytes(value.mObj.localSavePath, webRequest.downloadHandler.data);
  309. }
  310. catch (Exception)
  311. {
  312. ErrorLogPanel.Instance.Show(" 视频缓存出现错误 " + jsonString);
  313. }
  314. }
  315. }
  316. }
  317. /// <summary>
  318. /// Bundle 文件
  319. /// </summary>
  320. /// <param name="methodName"></param>
  321. /// <param name="jsonString"></param>
  322. /// <param name="savePath"></param>
  323. public void PostBundle(DownLoadMaterial mObj, string methodName, long time, string jsonString, Action<DownLoadMaterial, object> CallBack)
  324. {
  325. StartCoroutine(PostRequestBundle(mObj, methodName, time, jsonString, CallBack));
  326. }
  327. private IEnumerator PostRequestBundle(DownLoadMaterial mObj, string methodName, long time, string jsonString, Action<DownLoadMaterial, object> CallBack)
  328. {
  329. string url = baseUrl + methodName;
  330. //using (UnityWebRequest webRequestAB = UnityWebRequestAssetBundle.GetAssetBundle(url, (uint)time, 1))
  331. //{
  332. // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  333. // webRequestAB.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  334. // // webRequestAB.downloadHandler = (DownloadHandler)new DownloadHandlerFile(mObj.mObj.localSavePath);
  335. // yield return webRequestAB.SendWebRequest();
  336. // if (webRequestAB.isHttpError || webRequestAB.isNetworkError)
  337. // {
  338. // Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.data.Length);
  339. // }
  340. // else
  341. // {
  342. // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB);
  343. // GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
  344. // List<GameObject> listObjs = new List<GameObject>();
  345. // listObjs.Add(obj);
  346. // object value = listObjs;
  347. // CallBack(mObj, value);
  348. // }
  349. //}
  350. Dictionary<string, string> headers = new Dictionary<string, string>();
  351. headers["Content-Type"] = "application/json";
  352. headers["authorization"] = token;
  353. // webRequest.SetRequestHeader("authorization", token);
  354. //将文本转为byte数组
  355. byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes(jsonString);
  356. Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
  357. //向HTTP服务器提交Post数据
  358. WWW www = new WWW(url, bs, headers);
  359. //等待服务器的响应
  360. yield return www;
  361. if (www.error != null)
  362. {
  363. //获取服务器的错误信息
  364. Debug.LogError(www.error);
  365. ErrorLogPanel.Instance.Show(" 下载模型出现错误 " + jsonString);
  366. yield return null;
  367. }
  368. else
  369. {
  370. try
  371. {
  372. byte[] data = new byte[www.bytes.Length];
  373. data = www.bytes;
  374. Debug.Log(data.Length);
  375. string path = Application.persistentDataPath + "/Model";
  376. if (!Directory.Exists(path))
  377. {
  378. Directory.CreateDirectory(path);
  379. }
  380. File.WriteAllBytes(mObj.mObj.localSavePath, data);
  381. }
  382. catch (Exception e)
  383. {
  384. Debug.Log(e.Message.ToString());
  385. //ErrorLogPanel.Instance.Show(" 模型缓存出现错误 " + jsonString);
  386. }
  387. try
  388. {
  389. Debug.Log(www.bytes.Length);
  390. var ab = www.assetBundle;
  391. GameObject obj = ab.LoadAsset<GameObject>(ab.GetAllAssetNames()[0]);
  392. List<GameObject> listObjs = new List<GameObject>();
  393. listObjs.Add(obj);
  394. object value = listObjs;
  395. CallBack(mObj, value);
  396. }
  397. catch (Exception)
  398. {
  399. ErrorLogPanel.Instance.Show(" 模型生成出现错误 " + jsonString);
  400. }
  401. }
  402. //using (UnityWebRequest webRequestAB = new UnityWebRequest(url, "POST"))
  403. //{
  404. // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  405. // webRequestAB.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  406. // webRequestAB.downloadHandler = (DownloadHandler)new DownloadHandlerFile(mObj.mObj.localSavePath);
  407. // yield return webRequestAB.SendWebRequest();
  408. // if (webRequestAB.isHttpError || webRequestAB.isNetworkError)
  409. // {
  410. // Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.data.Length);
  411. // }
  412. // else
  413. // {
  414. // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB);
  415. // GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
  416. // List<GameObject> listObjs = new List<GameObject>();
  417. // listObjs.Add(obj);
  418. // object value = listObjs;
  419. // CallBack(mObj, value);
  420. // }
  421. //}
  422. }
  423. public void PostCDNImage(DownLoadMaterial value, string jsonString,Action<DownLoadMaterial,object> callback)
  424. {
  425. StartCoroutine(PostCDNRequestImage(value, jsonString, callback));
  426. }
  427. private IEnumerator PostCDNRequestImage(DownLoadMaterial value, string jsonString, Action<DownLoadMaterial, object> callback)
  428. {
  429. string url = value.mObj.cdnUrl;
  430. Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
  431. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  432. {
  433. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  434. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  435. DownloadHandlerTexture texture = new DownloadHandlerTexture(true);
  436. webRequest.downloadHandler = texture;
  437. // Debug.Log(value.mObj.localSavePath);
  438. // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath);
  439. // Debug.Log(value.mObj.localSavePath);
  440. webRequest.SetRequestHeader("authorization", token);
  441. foreach (var v in requestHeader)
  442. {
  443. webRequest.SetRequestHeader(v.Key, v.Value);
  444. }
  445. yield return webRequest.SendWebRequest();
  446. if (webRequest.isHttpError || webRequest.isNetworkError)
  447. {
  448. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  449. if (callback != null)
  450. {
  451. callback(value, null);
  452. }
  453. ErrorLogPanel.Instance.Show(" 下载图片出现错误 " + jsonString);
  454. }
  455. else
  456. {
  457. if (callback != null)
  458. {
  459. try
  460. {
  461. string path = Application.persistentDataPath + "/Image";
  462. if (!Directory.Exists(path))
  463. {
  464. Directory.CreateDirectory(path);
  465. }
  466. byte[] data = new byte[webRequest.downloadHandler.data.Length];
  467. data = webRequest.downloadHandler.data;
  468. File.WriteAllBytes(value.mObj.localSavePath, data);
  469. }
  470. catch (Exception e)
  471. {
  472. ErrorLogPanel.Instance.Show(" 图片缓存出现错误 " + jsonString);
  473. }
  474. try
  475. {
  476. Texture2D texture2 = texture.texture;
  477. Sprite createSprite = Sprite.Create(texture2, new Rect(0, 0, texture2.width, texture2.height), Vector2.zero);
  478. // Debug.Log(webRequest.downloadHandler.data.Length + " "+ texture2.width);
  479. callback(value, createSprite);
  480. Texture2D texture3 = texture.texture;
  481. }
  482. catch (Exception e)
  483. {
  484. ErrorLogPanel.Instance.Show(" 图片生成出现错误 " + jsonString);
  485. }
  486. ////转为字节数组
  487. // File.WriteAllBytes(value.mObj.localSavePath, webRequest.downloadHandler.data);
  488. }
  489. }
  490. }
  491. }
  492. public void PostCDNVideo(DownLoadMaterial value, string jsonString, Action<DownLoadMaterial, object> callback)
  493. {
  494. StartCoroutine(PostCDNRequestVideo(value, jsonString, callback));
  495. }
  496. private IEnumerator PostCDNRequestVideo(DownLoadMaterial value, string jsonString, Action<DownLoadMaterial, object> callback)
  497. {
  498. string url = value.mObj.cdnUrl;
  499. Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
  500. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  501. {
  502. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  503. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  504. // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerAudioClip(value.mObj.localSavePath, AudioType.ACC);
  505. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath);
  506. webRequest.SetRequestHeader("authorization", token);
  507. foreach (var v in requestHeader)
  508. {
  509. webRequest.SetRequestHeader(v.Key, v.Value);
  510. }
  511. yield return webRequest.SendWebRequest();
  512. if (webRequest.isHttpError || webRequest.isNetworkError)
  513. {
  514. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  515. callback(value, null);
  516. ErrorLogPanel.Instance.Show(" 下载视频出现错误 " + jsonString);
  517. }
  518. else
  519. {
  520. callback(value, value.mObj.localSavePath);
  521. // Debug.Log("文件下载成功 :" + value.mObj.DownloadPath +" "+ webRequest.downloadHandler.data.Length);
  522. //Debug.Log(webRequest.downloadHandler.data.Length);
  523. try
  524. {
  525. //string path = Application.persistentDataPath + "/Video";
  526. //if (!Directory.Exists(path))
  527. //{
  528. // Directory.CreateDirectory(path);
  529. //}
  530. //path = Application.persistentDataPath + "/StreamingAssets/Vuforia";
  531. //if (!Directory.Exists(path))
  532. //{
  533. // Directory.CreateDirectory(path);
  534. //}
  535. //File.WriteAllBytes(value.mObj.localSavePath, webRequest.downloadHandler.data);
  536. }
  537. catch (Exception)
  538. {
  539. ErrorLogPanel.Instance.Show(" 视频缓存出现错误 " + jsonString);
  540. }
  541. }
  542. }
  543. }
  544. /// <summary>
  545. /// Bundle 文件
  546. /// </summary>
  547. /// <param name="methodName"></param>
  548. /// <param name="jsonString"></param>
  549. /// <param name="savePath"></param>
  550. public void PostCDNBundle(DownLoadMaterial mObj, long time, string jsonString, Action<DownLoadMaterial, object> CallBack)
  551. {
  552. StartCoroutine(PostCDNRequestBundle(mObj, time, jsonString, CallBack));
  553. }
  554. private IEnumerator PostCDNRequestBundle(DownLoadMaterial mObj, long time, string jsonString, Action<DownLoadMaterial, object> CallBack)
  555. {
  556. string url = mObj.mObj.cdnUrl;
  557. //using (UnityWebRequest webRequestAB = UnityWebRequestAssetBundle.GetAssetBundle(url, (uint)time, 1))
  558. //{
  559. // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  560. // webRequestAB.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  561. // // webRequestAB.downloadHandler = (DownloadHandler)new DownloadHandlerFile(mObj.mObj.localSavePath);
  562. // yield return webRequestAB.SendWebRequest();
  563. // if (webRequestAB.isHttpError || webRequestAB.isNetworkError)
  564. // {
  565. // Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.data.Length);
  566. // }
  567. // else
  568. // {
  569. // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB);
  570. // GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
  571. // List<GameObject> listObjs = new List<GameObject>();
  572. // listObjs.Add(obj);
  573. // object value = listObjs;
  574. // CallBack(mObj, value);
  575. // }
  576. //}
  577. Dictionary<string, string> headers = new Dictionary<string, string>();
  578. headers["Content-Type"] = "application/json";
  579. headers["authorization"] = token;
  580. // webRequest.SetRequestHeader("authorization", token);
  581. //将文本转为byte数组
  582. byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes(jsonString);
  583. Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
  584. //向HTTP服务器提交Post数据
  585. WWW www = new WWW(url, bs, headers);
  586. //等待服务器的响应
  587. yield return www;
  588. if (www.error != null)
  589. {
  590. //获取服务器的错误信息
  591. Debug.LogError(www.error);
  592. ErrorLogPanel.Instance.Show(" 下载模型出现错误 " + jsonString);
  593. yield return null;
  594. }
  595. else
  596. {
  597. try
  598. {
  599. byte[] data = new byte[www.bytes.Length];
  600. data = www.bytes;
  601. Debug.Log(data.Length);
  602. string path = Application.persistentDataPath + "/Model";
  603. if (!Directory.Exists(path))
  604. {
  605. Directory.CreateDirectory(path);
  606. }
  607. File.WriteAllBytes(mObj.mObj.localSavePath, data);
  608. }
  609. catch (Exception e)
  610. {
  611. Debug.Log(e.Message.ToString());
  612. //ErrorLogPanel.Instance.Show(" 模型缓存出现错误 " + jsonString);
  613. }
  614. try
  615. {
  616. Debug.Log(www.bytes.Length);
  617. var ab = www.assetBundle;
  618. GameObject obj = ab.LoadAsset<GameObject>(ab.GetAllAssetNames()[0]);
  619. List<GameObject> listObjs = new List<GameObject>();
  620. listObjs.Add(obj);
  621. object value = listObjs;
  622. CallBack(mObj, value);
  623. }
  624. catch (Exception)
  625. {
  626. ErrorLogPanel.Instance.Show(" 模型生成出现错误 " + jsonString);
  627. }
  628. }
  629. //using (UnityWebRequest webRequestAB = new UnityWebRequest(url, "POST"))
  630. //{
  631. // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  632. // webRequestAB.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  633. // webRequestAB.downloadHandler = (DownloadHandler)new DownloadHandlerFile(mObj.mObj.localSavePath);
  634. // yield return webRequestAB.SendWebRequest();
  635. // if (webRequestAB.isHttpError || webRequestAB.isNetworkError)
  636. // {
  637. // Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.data.Length);
  638. // }
  639. // else
  640. // {
  641. // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB);
  642. // GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
  643. // List<GameObject> listObjs = new List<GameObject>();
  644. // listObjs.Add(obj);
  645. // object value = listObjs;
  646. // CallBack(mObj, value);
  647. // }
  648. //}
  649. }
  650. public void PostTest(string Loadpath, string jsonString, Action<string> CallBack)
  651. {
  652. StartCoroutine(PostRequest(Loadpath, jsonString, CallBack));
  653. }
  654. //
  655. private IEnumerator PostRequest(string loadPath, string jsonString, Action<string> CallBack)
  656. {
  657. string url = baseUrl + loadPath;
  658. Debug.Log(url);
  659. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  660. {
  661. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  662. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  663. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  664. webRequest.SetRequestHeader("authorization", token);
  665. foreach (var v in requestHeader)
  666. {
  667. webRequest.SetRequestHeader(v.Key, v.Value);
  668. // Debug.Log(v.Value + " " + loadPath);
  669. }
  670. yield return webRequest.SendWebRequest();
  671. if (webRequest.isHttpError || webRequest.isNetworkError)
  672. {
  673. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + loadPath+" "+ jsonString);
  674. GameManager.Instance.text2.text = jsonString + " " + webRequest.error;
  675. }
  676. else
  677. {
  678. // Debug.Log(webRequest.downloadHandler.text);
  679. CallBack(webRequest.downloadHandler.text);
  680. }
  681. }
  682. }
  683. public void GetAllMaterials(string methodName, string jsonString, Action<String> CallBack)
  684. {
  685. StartCoroutine(GetRequest(methodName, jsonString, CallBack));
  686. }
  687. private IEnumerator GetRequest(string methodName, string jsonString, Action<String> CallBack)
  688. {
  689. string url = baseUrl + methodName;
  690. Debug.Log(url);
  691. token = jsonString;
  692. HeadAddToken(token);
  693. using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
  694. {
  695. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  696. webRequest.SetRequestHeader("authorization", jsonString);
  697. foreach (var v in requestHeader)
  698. {
  699. Debug.Log(v.Key + " " + v.Value);
  700. webRequest.SetRequestHeader(v.Key, v.Value);
  701. }
  702. yield return webRequest.SendWebRequest();
  703. if (webRequest.isHttpError || webRequest.isNetworkError)
  704. {
  705. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  706. }
  707. else
  708. {
  709. Debug.Log(webRequest.downloadHandler.text);
  710. CallBack(webRequest.downloadHandler.text);
  711. }
  712. }
  713. }
  714. /// <summary>
  715. /// 登录
  716. /// </summary>
  717. /// <param name="methodName"></param>
  718. /// <param name="jsonString"></param>
  719. /// <param name="CallBack"></param>
  720. public void PostLogin(string methodName, string jsonString, Action<string> CallBack)
  721. {
  722. StartCoroutine(PostRequestLogin(methodName, jsonString, CallBack));
  723. }
  724. private IEnumerator PostRequestLogin(string methodName, string jsonString, Action<string> CallBack)
  725. {
  726. string url = baseUrl + methodName;
  727. Debug.Log(url);
  728. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  729. {
  730. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  731. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  732. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  733. foreach (var v in requestHeader)
  734. {
  735. webRequest.SetRequestHeader(v.Key, v.Value);
  736. }
  737. yield return webRequest.SendWebRequest();
  738. if (webRequest.isHttpError || webRequest.isNetworkError)
  739. {
  740. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  741. string error = webRequest.downloadHandler.text;
  742. JObject jObject = JObject.Parse(error);
  743. CallBack(jObject["message"].ToString());
  744. ErrorLogPanel.Instance.Show(" 请求后台数据出现错误 ");
  745. }
  746. else
  747. {
  748. Debug.Log(webRequest.downloadHandler.text);
  749. token = webRequest.downloadHandler.text;
  750. if (token.Contains("密码不正确,请重新输入") || token.Contains("用户未注册"))
  751. {
  752. CallBack(token);
  753. }
  754. else
  755. {
  756. JObject obj = JObject.Parse(token);
  757. Debug.Log(obj["data"]["token"].ToString());
  758. token = obj["data"]["token"].ToString();
  759. Debug.Log(token);
  760. CallBack(token);
  761. }
  762. }
  763. }
  764. }
  765. /// <summary>
  766. /// 获取本地Sprite
  767. /// </summary>
  768. /// <param name="mObj"></param>
  769. /// <param name="CallBack"></param>
  770. public void GetLocalSprite(DownLoadMaterial mObj, Action<DownLoadMaterial, object> CallBack)
  771. {
  772. // StartCoroutine(GetSpriteRequest(mObj, CallBack));
  773. try
  774. {
  775. var pathName = mObj.mObj.localSavePath;
  776. var bytes = ReadFile(pathName);
  777. int width = Screen.width;
  778. int height = Screen.height;
  779. var texture = new Texture2D(width, height);
  780. texture.LoadImage(bytes);
  781. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
  782. CallBack(mObj, sprite);
  783. }
  784. catch (Exception c)
  785. {
  786. ErrorLogPanel.Instance.Show(" 加载缓存图片出现错误 " + mObj.mObj.localSavePath);
  787. Debug.LogError(c.Message);
  788. }
  789. }
  790. private IEnumerator GetSpriteRequest(DownLoadMaterial mObj, Action<DownLoadMaterial, object> CallBack)
  791. {
  792. //WWW www = new WWW(mObj.mObj.localSavePath);
  793. //yield return www;
  794. //if (www.isDone && www.error == null)
  795. //{
  796. // Texture2D texture = www.texture;
  797. // Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
  798. // CallBack(mObj, sprite);
  799. //}
  800. //else
  801. //{
  802. // Debug.Log(mObj.mObj.localSavePath+" " + www.error);
  803. //}
  804. UnityWebRequest request = UnityWebRequestTexture.GetTexture(mObj.mObj.localSavePath);
  805. Debug.Log(mObj.mObj.localSavePath);
  806. yield return request.SendWebRequest();
  807. if (request.isNetworkError || request.isHttpError)
  808. {
  809. Debug.LogError(request.error);
  810. }
  811. else
  812. {
  813. Texture2D texture = DownloadHandlerTexture.GetContent(request);
  814. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
  815. CallBack(mObj, sprite);
  816. // LoadManager.Instance.DownLoadEnd(mObj, sprite);
  817. }
  818. }
  819. public byte[] ReadFile(string filePath)
  820. {
  821. var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  822. fs.Seek(0, SeekOrigin.Begin);
  823. var binary = new byte[fs.Length];
  824. fs.Read(binary, 0, binary.Length);
  825. fs.Close();
  826. return binary;
  827. }
  828. public void GetLocalModel(DownLoadMaterial mObj, Action<DownLoadMaterial, object> CallBack)
  829. {
  830. StartCoroutine(GetModelRequest(mObj,CallBack));
  831. }
  832. private IEnumerator GetModelRequest(DownLoadMaterial mObj, Action<DownLoadMaterial, object> CallBack)
  833. {
  834. Debug.Log(" Load LocalModel " + mObj.mObj.localSavePath);
  835. // UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(mObj.mObj.localSavePath);
  836. //// UnityWebRequest request = UnityWebRequest.Get(mObj.mObj.localSavePath);
  837. // yield return request.SendWebRequest();
  838. //if (request.isHttpError || request.isNetworkError)
  839. //{
  840. // Debug.LogError(request.error + "\n" + request.downloadHandler.text);
  841. // ErrorLogPanel.Instance.Show(" 加载缓存模型出现错误 " + mObj.mObj.localSavePath);
  842. //}
  843. AssetBundleCreateRequest request1 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(mObj.mObj.localSavePath));
  844. yield return request1;
  845. if(false)
  846. {
  847. }
  848. else
  849. {
  850. try
  851. {
  852. Debug.Log(" Load LocalModel2");
  853. //AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
  854. // AssetBundle bundle = request.downloadHandler.data;
  855. AssetBundle bundle = request1.assetBundle;
  856. GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
  857. List<GameObject> listObjs = new List<GameObject>();
  858. listObjs.Add(obj);
  859. LoadManager.Instance.DownLoadEnd(mObj, listObjs, true);
  860. bundle.Unload(false);
  861. object value = listObjs;
  862. CallBack(mObj, value);
  863. }
  864. catch (Exception)
  865. {
  866. ErrorLogPanel.Instance.Show(" 生成缓存模型出现错误 " + mObj.mObj.localSavePath);
  867. throw;
  868. }
  869. }
  870. }
  871. public void HeadAddToken(string token)
  872. {
  873. requestHeader.Add("x-token", token);
  874. }
  875. public string GetMd5Hash(string strToEncrypt)
  876. {
  877. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  878. byte[] bytes = Encoding.ASCII.GetBytes(strToEncrypt);
  879. byte[] encoded = md5.ComputeHash(bytes);
  880. StringBuilder sb = new StringBuilder();
  881. for (int i = 0; i < 10; i++)
  882. {
  883. sb.Append(encoded[i].ToString("x2"));
  884. }
  885. string password = sb.ToString();
  886. password = password.Substring(0, 10);
  887. return password;
  888. }
  889. }