1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using UnityEngine.Networking;
- using System.Text;
- using System.Security.Cryptography;
- using System.Net;
- using System.IO;
- using Newtonsoft.Json.Linq;
- /// <summary>
- /// Http Request SDK
- /// </summary>
- public class HttpTool : MonoBehaviour
- {
- private static HttpTool _instacne = null;
- // private string baseUrl = "http://office.ghz-tech.com:9981";
- // private string baseUrl = "https://webapi-fat.shadowcreator.com/100022";
- // private string baseUrl = "http://192.168.140.123:8080/guideSystem";
- // private string baseUrl = "http://office.ghz-tech.com:3419/guideSystem";
- // private string baseUrl = "http://office.ghz-tech.com:3425/guideSystem";
- private string baseUrl = "https://office.ghz-tech.com:3424/api";
- // private string baseUrl = "http://office.ghz-tech.com:9903/guideSystem";
- // private string baseUrl = "http://ghz-tech.com:3419/guideSystem";
- // private string baseUrl = "http://192.168.43.142:8080";
- private string sKey = "zoo_visit_key";
- private string token = "";
- Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
- public static HttpTool Instance
- {
- get
- {
- if (_instacne == null)
- {
- Debug.LogError("Awake error");
- // HttpTool._instacne = gameObject.GetComponent<HttpTool>();
- }
- return _instacne;
- }
- }
- void Awake()
- {
- DontDestroyOnLoad(gameObject);
- HttpTool._instacne = gameObject.GetComponent<HttpTool>();
- //http header 的内容
- requestHeader.Add("Content-Type", "application/json");
- // requestHeader.Add("sKey", sKey);
- }
- public void Get(string methodName, Action<string> callback)
- {
- StartCoroutine(GetRequest(methodName, callback));
- }
- private IEnumerator GetRequest(string methodName, Action<string> callback)
- {
- // string url = Application.streamingAssetsPath + methodName;
- string url = baseUrl + methodName;
- Debug.Log(url);
- using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
- {
- webRequest.SetRequestHeader("authorization", token);
- //设置header
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- ErrorLogPanel.Instance.Show(" 请求后台数据出现错误 ");
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
- if (callback != null)
- {
- callback(null);
-
- }
- }
- else
- {
- if (callback != null)
- {
- callback(webRequest.downloadHandler.text);
- }
- }
- }
- }
- //public void Post( string methodName, string jsonString,Action<String> callback)
- //{
- // StartCoroutine(Post( methodName, jsonString, callback));
- //}
- //private IEnumerator PostRequest(string methodName, string jsonString, Action<String> callback)
- //{
- //}
- //jsonString 为json字符串,post提交的数据包为json
- // Loging 成功获取token 失败返回重新登录
- // 选择场景 根据选择的场景,获取当前场景的所有素材信息
- // 第一次下载 如果有素材之前没有下载过的,在这里需要进行下载/更新
- /// <summary>
- /// 文字
- /// </summary>
- /// <param name="methodName"></param>
- /// <param name="jsonString"></param>
- /// <param name="callback"></param>
- public void PostTxt(DownLoadMaterial value, string methodName, string jsonString, Action<DownLoadMaterial, object> callback)
- {
- StartCoroutine(PostRequestTxt(value, methodName, jsonString, callback));
- }
- private IEnumerator PostRequestTxt(DownLoadMaterial value, string methodName, string jsonString, Action<DownLoadMaterial, object> callback)
- {
- string url = baseUrl + methodName;
- Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
- {
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath);
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- ErrorLogPanel.Instance.Show(" 请求后台数据出现错误 ");
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
- if (callback != null)
- {
- callback(value, null);
- }
- }
- else
- {
- if (callback != null)
- {
- File.WriteAllBytes(Application.persistentDataPath + "/Text/" + methodName, webRequest.downloadHandler.data);
- callback(value, webRequest.downloadHandler.text);
- }
- }
- }
- }
- #region Old
- //public void Post(string methodName, string jsonString,string function, Action<string ,string> callback)
- //{
- // StartCoroutine(PostRequest(methodName, jsonString, function, callback));
- //}
- //private IEnumerator PostRequest(string methodName, string jsonString, string function, Action<string ,string> callback)
- //{
- // string url = baseUrl + methodName;
- // Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
- // using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
- // {
- // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
- // webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
- // foreach (var v in requestHeader)
- // {
- // webRequest.SetRequestHeader(v.Key, v.Value);
- // }
- // yield return webRequest.SendWebRequest();
- // if (webRequest.isHttpError || webRequest.isNetworkError)
- // {
- // Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
- // if (callback != null)
- // {
- // callback(function,null);
- // }
- // }
- // else
- // {
- // if (callback != null)
- // {
- // callback(function,webRequest.downloadHandler.text);
- // }
- // }
- // }
- //}
- #endregion
- /// <summary>
- /// 图片
- /// </summary>
- /// <param name="methodName"></param>
- /// <param name="jsonString"></param>
- /// <param name="callback"></param>
- public void PostImage(DownLoadMaterial value, string methodName, string jsonString, Action<DownLoadMaterial, object> callback)
- {
- StartCoroutine(PostRequestImage(value, methodName, jsonString, callback));
- }
- private IEnumerator PostRequestImage(DownLoadMaterial value, string methodName, string jsonString, Action<DownLoadMaterial, object> callback)
- {
- string url = baseUrl + methodName;
- Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
- {
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- DownloadHandlerTexture texture = new DownloadHandlerTexture(true);
- webRequest.downloadHandler = texture;
- // Debug.Log(value.mObj.localSavePath);
- // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath);
- // Debug.Log(value.mObj.localSavePath);
- webRequest.SetRequestHeader("authorization", token);
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
- if (callback != null)
- {
- callback(value, null);
- }
- ErrorLogPanel.Instance.Show(" 下载图片出现错误 " + jsonString);
- }
- else
- {
- if (callback != null)
- {
- try
- {
- string path = Application.persistentDataPath + "/Image";
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- byte[] data = new byte[webRequest.downloadHandler.data.Length];
- data = webRequest.downloadHandler.data;
- File.WriteAllBytes(value.mObj.localSavePath, data);
- }
- catch (Exception e)
- {
- ErrorLogPanel.Instance.Show(" 图片缓存出现错误 " + jsonString);
- }
- try
- {
- Texture2D texture2 = texture.texture;
- Sprite createSprite = Sprite.Create(texture2, new Rect(0, 0, texture2.width, texture2.height), Vector2.zero);
- // Debug.Log(webRequest.downloadHandler.data.Length + " "+ texture2.width);
- callback(value, createSprite);
- Texture2D texture3 = texture.texture;
- }
- catch (Exception e)
- {
- ErrorLogPanel.Instance.Show(" 图片生成出现错误 " + jsonString);
- }
- ////转为字节数组
- // File.WriteAllBytes(value.mObj.localSavePath, webRequest.downloadHandler.data);
- }
- }
- }
- }
- /// <summary>
- /// 视频
- /// </summary>
- /// <param name="methodName"></param>
- /// <param name="jsonString"></param>
- /// <param name="savePath"></param>
- public void PostVideo(DownLoadMaterial value, string methodName, string jsonString, Action<DownLoadMaterial, object> callback)
- {
- StartCoroutine(PostRequestVideo(value, methodName, jsonString, callback));
- }
- private IEnumerator PostRequestVideo(DownLoadMaterial value, string methodName, string jsonString, Action<DownLoadMaterial, object> callback)
- {
- string url = baseUrl + methodName;
- Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
- {
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerAudioClip(value.mObj.localSavePath, AudioType.ACC);
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath);
- webRequest.SetRequestHeader("authorization", token);
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
- callback(value, null);
- ErrorLogPanel.Instance.Show(" 下载视频出现错误 " + jsonString);
- }
- else
- {
- callback(value, value.mObj.localSavePath);
- // Debug.Log("文件下载成功 :" + value.mObj.DownloadPath +" "+ webRequest.downloadHandler.data.Length);
- //Debug.Log(webRequest.downloadHandler.data.Length);
- try
- {
- //string path = Application.persistentDataPath + "/Video";
- //if (!Directory.Exists(path))
- //{
- // Directory.CreateDirectory(path);
- //}
- //path = Application.persistentDataPath + "/StreamingAssets/Vuforia";
- //if (!Directory.Exists(path))
- //{
- // Directory.CreateDirectory(path);
- //}
- //File.WriteAllBytes(value.mObj.localSavePath, webRequest.downloadHandler.data);
- }
- catch (Exception)
- {
- ErrorLogPanel.Instance.Show(" 视频缓存出现错误 " + jsonString);
- }
- }
- }
- }
- /// <summary>
- /// Bundle 文件
- /// </summary>
- /// <param name="methodName"></param>
- /// <param name="jsonString"></param>
- /// <param name="savePath"></param>
- public void PostBundle(DownLoadMaterial mObj, string methodName, long time, string jsonString, Action<DownLoadMaterial, object> CallBack)
- {
- StartCoroutine(PostRequestBundle(mObj, methodName, time, jsonString, CallBack));
- }
- private IEnumerator PostRequestBundle(DownLoadMaterial mObj, string methodName, long time, string jsonString, Action<DownLoadMaterial, object> CallBack)
- {
- string url = baseUrl + methodName;
- //using (UnityWebRequest webRequestAB = UnityWebRequestAssetBundle.GetAssetBundle(url, (uint)time, 1))
- //{
- // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
- // webRequestAB.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- // // webRequestAB.downloadHandler = (DownloadHandler)new DownloadHandlerFile(mObj.mObj.localSavePath);
- // yield return webRequestAB.SendWebRequest();
- // if (webRequestAB.isHttpError || webRequestAB.isNetworkError)
- // {
- // Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.data.Length);
- // }
- // else
- // {
- // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB);
- // GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
- // List<GameObject> listObjs = new List<GameObject>();
- // listObjs.Add(obj);
- // object value = listObjs;
- // CallBack(mObj, value);
- // }
- //}
- Dictionary<string, string> headers = new Dictionary<string, string>();
- headers["Content-Type"] = "application/json";
- headers["authorization"] = token;
- // webRequest.SetRequestHeader("authorization", token);
- //将文本转为byte数组
- byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes(jsonString);
- Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
- //向HTTP服务器提交Post数据
- WWW www = new WWW(url, bs, headers);
- //等待服务器的响应
- yield return www;
- if (www.error != null)
- {
- //获取服务器的错误信息
- Debug.LogError(www.error);
- ErrorLogPanel.Instance.Show(" 下载模型出现错误 " + jsonString);
- yield return null;
- }
- else
- {
- try
- {
- byte[] data = new byte[www.bytes.Length];
- data = www.bytes;
- Debug.Log(data.Length);
- string path = Application.persistentDataPath + "/Model";
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- File.WriteAllBytes(mObj.mObj.localSavePath, data);
- }
- catch (Exception e)
- {
- Debug.Log(e.Message.ToString());
- //ErrorLogPanel.Instance.Show(" 模型缓存出现错误 " + jsonString);
- }
- try
- {
- Debug.Log(www.bytes.Length);
- var ab = www.assetBundle;
- GameObject obj = ab.LoadAsset<GameObject>(ab.GetAllAssetNames()[0]);
- List<GameObject> listObjs = new List<GameObject>();
- listObjs.Add(obj);
- object value = listObjs;
- CallBack(mObj, value);
- }
- catch (Exception)
- {
- ErrorLogPanel.Instance.Show(" 模型生成出现错误 " + jsonString);
- }
- }
- //using (UnityWebRequest webRequestAB = new UnityWebRequest(url, "POST"))
- //{
- // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
- // webRequestAB.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- // webRequestAB.downloadHandler = (DownloadHandler)new DownloadHandlerFile(mObj.mObj.localSavePath);
- // yield return webRequestAB.SendWebRequest();
- // if (webRequestAB.isHttpError || webRequestAB.isNetworkError)
- // {
- // Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.data.Length);
- // }
- // else
- // {
- // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB);
- // GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
- // List<GameObject> listObjs = new List<GameObject>();
- // listObjs.Add(obj);
- // object value = listObjs;
- // CallBack(mObj, value);
- // }
- //}
- }
-
- public void PostCDNImage(DownLoadMaterial value, string jsonString,Action<DownLoadMaterial,object> callback)
- {
- StartCoroutine(PostCDNRequestImage(value, jsonString, callback));
- }
- private IEnumerator PostCDNRequestImage(DownLoadMaterial value, string jsonString, Action<DownLoadMaterial, object> callback)
- {
- string url = value.mObj.cdnUrl;
- Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
- {
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- DownloadHandlerTexture texture = new DownloadHandlerTexture(true);
- webRequest.downloadHandler = texture;
- // Debug.Log(value.mObj.localSavePath);
- // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath);
- // Debug.Log(value.mObj.localSavePath);
- webRequest.SetRequestHeader("authorization", token);
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
- if (callback != null)
- {
- callback(value, null);
- }
- ErrorLogPanel.Instance.Show(" 下载图片出现错误 " + jsonString);
- }
- else
- {
- if (callback != null)
- {
- try
- {
- string path = Application.persistentDataPath + "/Image";
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- byte[] data = new byte[webRequest.downloadHandler.data.Length];
- data = webRequest.downloadHandler.data;
- File.WriteAllBytes(value.mObj.localSavePath, data);
- }
- catch (Exception e)
- {
- ErrorLogPanel.Instance.Show(" 图片缓存出现错误 " + jsonString);
- }
- try
- {
- Texture2D texture2 = texture.texture;
- Sprite createSprite = Sprite.Create(texture2, new Rect(0, 0, texture2.width, texture2.height), Vector2.zero);
- // Debug.Log(webRequest.downloadHandler.data.Length + " "+ texture2.width);
- callback(value, createSprite);
- Texture2D texture3 = texture.texture;
- }
- catch (Exception e)
- {
- ErrorLogPanel.Instance.Show(" 图片生成出现错误 " + jsonString);
- }
- ////转为字节数组
- // File.WriteAllBytes(value.mObj.localSavePath, webRequest.downloadHandler.data);
- }
- }
- }
- }
- public void PostCDNVideo(DownLoadMaterial value, string jsonString, Action<DownLoadMaterial, object> callback)
- {
- StartCoroutine(PostCDNRequestVideo(value, jsonString, callback));
- }
- private IEnumerator PostCDNRequestVideo(DownLoadMaterial value, string jsonString, Action<DownLoadMaterial, object> callback)
- {
- string url = value.mObj.cdnUrl;
- Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
- {
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerAudioClip(value.mObj.localSavePath, AudioType.ACC);
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath);
- webRequest.SetRequestHeader("authorization", token);
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
- callback(value, null);
- ErrorLogPanel.Instance.Show(" 下载视频出现错误 " + jsonString);
- }
- else
- {
- callback(value, value.mObj.localSavePath);
- // Debug.Log("文件下载成功 :" + value.mObj.DownloadPath +" "+ webRequest.downloadHandler.data.Length);
- //Debug.Log(webRequest.downloadHandler.data.Length);
- try
- {
- //string path = Application.persistentDataPath + "/Video";
- //if (!Directory.Exists(path))
- //{
- // Directory.CreateDirectory(path);
- //}
- //path = Application.persistentDataPath + "/StreamingAssets/Vuforia";
- //if (!Directory.Exists(path))
- //{
- // Directory.CreateDirectory(path);
- //}
- //File.WriteAllBytes(value.mObj.localSavePath, webRequest.downloadHandler.data);
- }
- catch (Exception)
- {
- ErrorLogPanel.Instance.Show(" 视频缓存出现错误 " + jsonString);
- }
- }
- }
- }
- /// <summary>
- /// Bundle 文件
- /// </summary>
- /// <param name="methodName"></param>
- /// <param name="jsonString"></param>
- /// <param name="savePath"></param>
- public void PostCDNBundle(DownLoadMaterial mObj, long time, string jsonString, Action<DownLoadMaterial, object> CallBack)
- {
- StartCoroutine(PostCDNRequestBundle(mObj, time, jsonString, CallBack));
- }
- private IEnumerator PostCDNRequestBundle(DownLoadMaterial mObj, long time, string jsonString, Action<DownLoadMaterial, object> CallBack)
- {
- string url = mObj.mObj.cdnUrl;
- //using (UnityWebRequest webRequestAB = UnityWebRequestAssetBundle.GetAssetBundle(url, (uint)time, 1))
- //{
- // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
- // webRequestAB.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- // // webRequestAB.downloadHandler = (DownloadHandler)new DownloadHandlerFile(mObj.mObj.localSavePath);
- // yield return webRequestAB.SendWebRequest();
- // if (webRequestAB.isHttpError || webRequestAB.isNetworkError)
- // {
- // Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.data.Length);
- // }
- // else
- // {
- // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB);
- // GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
- // List<GameObject> listObjs = new List<GameObject>();
- // listObjs.Add(obj);
- // object value = listObjs;
- // CallBack(mObj, value);
- // }
- //}
- Dictionary<string, string> headers = new Dictionary<string, string>();
- headers["Content-Type"] = "application/json";
- headers["authorization"] = token;
- // webRequest.SetRequestHeader("authorization", token);
- //将文本转为byte数组
- byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes(jsonString);
- Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString));
- //向HTTP服务器提交Post数据
- WWW www = new WWW(url, bs, headers);
- //等待服务器的响应
- yield return www;
- if (www.error != null)
- {
- //获取服务器的错误信息
- Debug.LogError(www.error);
- ErrorLogPanel.Instance.Show(" 下载模型出现错误 " + jsonString);
- yield return null;
- }
- else
- {
- try
- {
- byte[] data = new byte[www.bytes.Length];
- data = www.bytes;
- Debug.Log(data.Length);
- string path = Application.persistentDataPath + "/Model";
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- File.WriteAllBytes(mObj.mObj.localSavePath, data);
- }
- catch (Exception e)
- {
- Debug.Log(e.Message.ToString());
- //ErrorLogPanel.Instance.Show(" 模型缓存出现错误 " + jsonString);
- }
- try
- {
- Debug.Log(www.bytes.Length);
- var ab = www.assetBundle;
- GameObject obj = ab.LoadAsset<GameObject>(ab.GetAllAssetNames()[0]);
- List<GameObject> listObjs = new List<GameObject>();
- listObjs.Add(obj);
- object value = listObjs;
- CallBack(mObj, value);
- }
- catch (Exception)
- {
- ErrorLogPanel.Instance.Show(" 模型生成出现错误 " + jsonString);
- }
- }
- //using (UnityWebRequest webRequestAB = new UnityWebRequest(url, "POST"))
- //{
- // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
- // webRequestAB.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- // webRequestAB.downloadHandler = (DownloadHandler)new DownloadHandlerFile(mObj.mObj.localSavePath);
- // yield return webRequestAB.SendWebRequest();
- // if (webRequestAB.isHttpError || webRequestAB.isNetworkError)
- // {
- // Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.data.Length);
- // }
- // else
- // {
- // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB);
- // GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
- // List<GameObject> listObjs = new List<GameObject>();
- // listObjs.Add(obj);
- // object value = listObjs;
- // CallBack(mObj, value);
- // }
- //}
- }
- public void PostTest(string Loadpath, string jsonString, Action<string> CallBack)
- {
- StartCoroutine(PostRequest(Loadpath, jsonString, CallBack));
- }
- //
- private IEnumerator PostRequest(string loadPath, string jsonString, Action<string> CallBack)
- {
- string url = baseUrl + loadPath;
- Debug.Log(url);
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
- {
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
- webRequest.SetRequestHeader("authorization", token);
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- // Debug.Log(v.Value + " " + loadPath);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + loadPath+" "+ jsonString);
- GameManager.Instance.text2.text = jsonString + " " + webRequest.error;
- }
- else
- {
- // Debug.Log(webRequest.downloadHandler.text);
- CallBack(webRequest.downloadHandler.text);
- }
- }
- }
- public void GetAllMaterials(string methodName, string jsonString, Action<String> CallBack)
- {
- StartCoroutine(GetRequest(methodName, jsonString, CallBack));
- }
- private IEnumerator GetRequest(string methodName, string jsonString, Action<String> CallBack)
- {
- string url = baseUrl + methodName;
- Debug.Log(url);
- token = jsonString;
- HeadAddToken(token);
- using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
- {
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
- webRequest.SetRequestHeader("authorization", jsonString);
- foreach (var v in requestHeader)
- {
- Debug.Log(v.Key + " " + v.Value);
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
- }
- else
- {
- Debug.Log(webRequest.downloadHandler.text);
- CallBack(webRequest.downloadHandler.text);
- }
- }
- }
- /// <summary>
- /// 登录
- /// </summary>
- /// <param name="methodName"></param>
- /// <param name="jsonString"></param>
- /// <param name="CallBack"></param>
- public void PostLogin(string methodName, string jsonString, Action<string> CallBack)
- {
- StartCoroutine(PostRequestLogin(methodName, jsonString, CallBack));
- }
- private IEnumerator PostRequestLogin(string methodName, string jsonString, Action<string> CallBack)
- {
- string url = baseUrl + methodName;
- Debug.Log(jsonString);
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
- {
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
- string error = webRequest.downloadHandler.text;
- JObject jObject = JObject.Parse(error);
- CallBack(jObject["message"].ToString());
- ErrorLogPanel.Instance.Show(" 请求后台数据出现错误 ");
- }
- else
- {
- Debug.Log(webRequest.downloadHandler.text);
- token = webRequest.downloadHandler.text;
- if (token.Contains("密码不正确,请重新输入") || token.Contains("用户未注册"))
- {
- CallBack(token);
- }
- else
- {
- JObject obj = JObject.Parse(token);
- Debug.Log(obj["data"]["token"].ToString());
- token = obj["data"]["token"].ToString();
- Debug.Log(token);
- CallBack(token);
- }
- }
- }
- }
- /// <summary>
- /// 获取本地Sprite
- /// </summary>
- /// <param name="mObj"></param>
- /// <param name="CallBack"></param>
- public void GetLocalSprite(DownLoadMaterial mObj, Action<DownLoadMaterial, object> CallBack)
- {
- // StartCoroutine(GetSpriteRequest(mObj, CallBack));
- try
- {
- var pathName = mObj.mObj.localSavePath;
- var bytes = ReadFile(pathName);
- int width = Screen.width;
- int height = Screen.height;
- var texture = new Texture2D(width, height);
- texture.LoadImage(bytes);
- Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
- CallBack(mObj, sprite);
- }
- catch (Exception c)
- {
- ErrorLogPanel.Instance.Show(" 加载缓存图片出现错误 " + mObj.mObj.localSavePath);
- Debug.LogError(c.Message);
- }
- }
- private IEnumerator GetSpriteRequest(DownLoadMaterial mObj, Action<DownLoadMaterial, object> CallBack)
- {
- //WWW www = new WWW(mObj.mObj.localSavePath);
- //yield return www;
- //if (www.isDone && www.error == null)
- //{
- // Texture2D texture = www.texture;
- // Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
- // CallBack(mObj, sprite);
- //}
- //else
- //{
- // Debug.Log(mObj.mObj.localSavePath+" " + www.error);
- //}
- UnityWebRequest request = UnityWebRequestTexture.GetTexture(mObj.mObj.localSavePath);
- Debug.Log(mObj.mObj.localSavePath);
- yield return request.SendWebRequest();
- if (request.isNetworkError || request.isHttpError)
- {
- Debug.LogError(request.error);
- }
- else
- {
- Texture2D texture = DownloadHandlerTexture.GetContent(request);
- Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
- CallBack(mObj, sprite);
- // LoadManager.Instance.DownLoadEnd(mObj, sprite);
- }
- }
- public byte[] ReadFile(string filePath)
- {
- var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
- fs.Seek(0, SeekOrigin.Begin);
- var binary = new byte[fs.Length];
- fs.Read(binary, 0, binary.Length);
- fs.Close();
- return binary;
- }
- public void GetLocalModel(DownLoadMaterial mObj, Action<DownLoadMaterial, object> CallBack)
- {
- StartCoroutine(GetModelRequest(mObj, CallBack));
- }
- private IEnumerator GetModelRequest(DownLoadMaterial mObj, Action<DownLoadMaterial, object> CallBack)
- {
- //Debug.Log(" Load LocalModel");
- //UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(mObj.mObj.localSavePath);
- //yield return request.SendWebRequest();
- //if (request.isHttpError || request.isNetworkError)
- //{
- // Debug.LogError(request.error + "\n" + request.downloadHandler.text);
- // ErrorLogPanel.Instance.Show(" 加载缓存模型出现错误 " + mObj.mObj.localSavePath);
- //}
- //else
- //{
- // try
- // {
- // Debug.Log(" Load LocalModel2");
- // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
- // GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
- // List<GameObject> listObjs = new List<GameObject>();
- // listObjs.Add(obj);
- // LoadManager.Instance.DownLoadEnd(mObj, listObjs);
- // bundle.Unload(false);
- // object value = listObjs;
- // CallBack(mObj, value);
- // }
- // catch (Exception)
- // {
- // ErrorLogPanel.Instance.Show(" 生成缓存模型出现错误 " + mObj.mObj.localSavePath);
- // throw;
- // }
- //}
- AssetBundleCreateRequest request1 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(mObj.mObj.localSavePath));
- yield return request1;
- if (false)
- {
- }
- else
- {
- try
- {
- Debug.Log(" Load LocalModel2");
- //AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
- // AssetBundle bundle = request.downloadHandler.data;
- AssetBundle bundle = request1.assetBundle;
- GameObject obj = bundle.LoadAsset<GameObject>(bundle.GetAllAssetNames()[0]);
- List<GameObject> listObjs = new List<GameObject>();
- listObjs.Add(obj);
- LoadManager.Instance.DownLoadEnd(mObj, listObjs);
- bundle.Unload(false);
- object value = listObjs;
- CallBack(mObj, value);
- }
- catch (Exception)
- {
- ErrorLogPanel.Instance.Show(" 生成缓存模型出现错误 " + mObj.mObj.localSavePath);
- throw;
- }
- }
- }
- public void HeadAddToken(string token)
- {
- requestHeader.Add("x-token", token);
- }
- public string GetMd5Hash(string strToEncrypt)
- {
- MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
- byte[] bytes = Encoding.ASCII.GetBytes(strToEncrypt);
- byte[] encoded = md5.ComputeHash(bytes);
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < 10; i++)
- {
- sb.Append(encoded[i].ToString("x2"));
- }
- string password = sb.ToString();
- password = password.Substring(0, 10);
- return password;
- }
- }
|