using login; using System; using System.Collections; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; using UnityEngine; using UnityEngine.Networking; using XRTool.Util; public class HttpToolLangChao : MonoSingleton { private Dictionary requestHeader = new Dictionary(); public Dictionary RequestHeader => requestHeader; public void PostTest(string methodName, string jsonString, Action CallBack) { StartCoroutine(SendHttp(methodName, jsonString, CallBack)); } public void Post(string methodName, string jsonString, Action CallBack) { StartCoroutine(SendHttp(methodName, jsonString, CallBack)); } public void PostLogin(string methodName, string jsonString, Action CallBack) { StartCoroutine(SendHttp(methodName, jsonString, CallBack)); } public void initHead(string ContentType) { requestHeader.Clear(); if (Singleton.Instance.Token != "" && Singleton.Instance.Token != null) { requestHeader.Add("x-token", Singleton.Instance.Token); //requestHeader.Add("x-token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjQ4NzQxMTY1MDIsImlhdCI6MTcyMDUxNjUwMiwidXNlciI6eyJpZCI6MTA5MiwiZW1haWwiOiIiLCJwaG9uZSI6InprZGVtbyIsIm5hbWUiOiJ6a2RlbW8iLCJyb2xlSWQiOjF9fQ.OZ2HDUp9F5S504cmCUh7Ai15sq_nQ5i7ZG_odxhqGGM"); requestHeader.Add("authorization", Singleton.Instance.Token); } requestHeader.Add("Content-Type", ContentType); } public IEnumerator SendHttp(string methodName, string jsonString, Action CallBack, string ContentType = "application/json", bool isPost = true) { Debug.Log("Start Queue SendHttp " + methodName); string text = (!methodName.Contains("http")) ? ("https://api-fat2.ghz-tech.com" + methodName) : methodName; Debug.Log("URL:" + text + " isPost " + isPost); UnityWebRequest webRequest = isPost ? new UnityWebRequest(text, "POST") : UnityWebRequest.Get(text); initHead(ContentType); using (webRequest) { if (!isPost) { webRequest.downloadHandler = new DownloadHandlerBuffer(); foreach (KeyValuePair item in requestHeader) { webRequest.SetRequestHeader(item.Key, item.Value); } } else { byte[] bytes = Encoding.UTF8.GetBytes(jsonString); webRequest.uploadHandler = new UploadHandlerRaw(bytes); webRequest.downloadHandler = new DownloadHandlerBuffer(); } foreach (KeyValuePair item2 in requestHeader) { webRequest.SetRequestHeader(item2.Key, item2.Value); } yield return webRequest.SendWebRequest(); Debug.Log("CallBack==>" + webRequest.downloadHandler.text); if (webRequest.isHttpError || webRequest.isNetworkError) { string text2 = webRequest.downloadHandler.text; CallBack(text2); } else { string text3 = webRequest.downloadHandler.text; CallBack(text3); } } } public void HeadAddToken(string token) { requestHeader.Add("x-token", token); } public string GetMd5Hash(string strToEncrypt) { MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider(); byte[] bytes = Encoding.ASCII.GetBytes(strToEncrypt); byte[] array = mD5CryptoServiceProvider.ComputeHash(bytes); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < 10; i++) { stringBuilder.Append(array[i].ToString("x2")); } return stringBuilder.ToString().Substring(0, 10); } }