123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- 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 : MonoSingleton<HttpTool>
- {
- // private string baseUrl = "http://office.ghz-tech.com:3425/guideSystem";
- //private string baseUrl = "http://office.ghz-tech.com:9904/api";
- private string baseUrl = "https://nav21.ghz-tech.com:8843/api/";
- private string UrlPointCloud = "https://pro.qulivr.com/mr-navigator/v1";
- private string token = "";
- Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
- void Awake()
- {
- //http header 的内容
- requestHeader.Add("Content-Type", "application/json");
- }
- public string Token
- {
- get { return token; }
- }
- public string BaseUrl
- {
- get { return baseUrl; }
- }
- public Dictionary<string, string> RequestHeader
- {
- get { return requestHeader; }
- }
- 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:"+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)
- {
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
- if (callback != null)
- {
- callback(null);
- }
- }
- else
- {
- if (callback != null)
- {
- callback(webRequest.downloadHandler.text);
- }
- }
- }
- }
- public void PostTest(string methodName, string jsonString, Action<string> CallBack)
- {
- StartCoroutine(PostRequestTest(methodName, jsonString, CallBack));
- }
- private IEnumerator PostRequestTest(string methodName, string jsonString, Action<string> CallBack)
- {
- string url = baseUrl + methodName;
- Debug.Log(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();
- webRequest.SetRequestHeader("authorization", token);
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- Debug.Log(v.Value + " " + methodName);
- }
- webRequest.timeout = 5;
- yield return webRequest.SendWebRequest();
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- CallBack?.Invoke("UnityWebRequest Error");
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
- //GameManager.Instance.text2.text = webRequest.error;
- }
- else
- {
- Debug.Log(webRequest.downloadHandler.text);
- CallBack(webRequest.downloadHandler.text);
- }
- }
- }
- public void PostSnInfo(string methodName, string jsonString, int index, Action<int, string> CallBack)
- {
- StartCoroutine(PostnInfoRequest(methodName, jsonString, index, CallBack));
- }
- public IEnumerator PostnInfoRequest(string methodName, string jsonString, int index, Action<int, string> CallBack)
- {
- string url = baseUrl + methodName;
- Debug.Log("URL:" + 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 + " " + methodName);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
- //GameManager.Instance.text2.text = webRequest.error;
- }
- else
- {
- Debug.Log(webRequest.downloadHandler.text);
- CallBack(index, webRequest.downloadHandler.text);
- }
- }
- }
- public IEnumerator PostRequestSpoitDetail(string methodName, string jsonString, Action<string> CallBack)
- {
- string url = baseUrl + methodName;
- 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 + " " + methodName);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
- //GameManager.Instance.text2.text = webRequest.error;
- }
- else
- {
- //Debug.Log(webRequest.downloadHandler.text);
- CallBack(webRequest.downloadHandler.text);
- }
- }
- }
- public void Post(string methodName, string jsonString, Action<string> CallBack)
- {
- StartCoroutine(PostRequest(methodName, jsonString, CallBack));
- }
- //
- private IEnumerator PostRequest(string methodName, string jsonString, Action<string> CallBack)
- {
- string url = baseUrl + methodName;
- //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 + " " + methodName);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
- //GameManager.Instance.text2.text = 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:"+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("URL:" + 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();
- 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());
- }
- else
- {
- //Debug.Log(webRequest.downloadHandler.text);
- var mes = webRequest.downloadHandler.text;
- JObject obj = JObject.Parse(mes);
- if (obj["code"].ToString() == "200")
- {
- //Debug.Log(obj["data"]["token"].ToString());
- token = obj["data"]["token"].ToString();
- //Debug.Log(mes);
- }
- CallBack(mes);
- }
- }
- }
- ///// <summary>
- ///// 获取本地Sprite
- ///// </summary>
- ///// <param name="mObj"></param>
- ///// <param name="CallBack"></param>
- //public void GetLocalSprite(DownLoadMaterial mObj, Action<DownLoadMaterial, object> CallBack)
- //{
- // StartCoroutine(GetSpriteRequest(mObj, CallBack));
- //}
- //private IEnumerator GetSpriteRequest(DownLoadMaterial mObj, Action<DownLoadMaterial, object> CallBack)
- //{
- // UnityWebRequest request = UnityWebRequestTexture.GetTexture(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 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;
- }
- }
|