123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074 |
- 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");
- }
- 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;
- }
- }
|