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; /// /// Http Request SDK /// public class HttpTool : MonoSingleton { Dictionary requestHeader = new Dictionary(); // header void Awake() { //http header 的内容 requestHeader.Add("Content-Type", "application/json"); } // public string Token // { // get { return token; } // } public Dictionary RequestHeader { get { return requestHeader; } } public void Get(string methodName, Action callback) { StartCoroutine(SendHttp(methodName,"", callback,false)); } 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 GetAllMaterials(string methodName, string jsonString, Action CallBack) { StartCoroutine(SendHttp(methodName, "", CallBack,false)); } /// /// 登录 /// /// /// /// public void PostLogin(string methodName, string jsonString, Action CallBack) { StartCoroutine(SendHttp(methodName, jsonString, CallBack)); } public void initHead() { requestHeader.Clear(); if (UserInfo.Instance.Token!="" && UserInfo.Instance.Token!=null) { requestHeader.Add("x-token", UserInfo.Instance.Token); requestHeader.Add("authorization", UserInfo.Instance.Token); } requestHeader.Add("Content-Type", "application/json"); } public IEnumerator SendHttp(string methodName, string jsonString, Action CallBack,bool isPost=true) { //Debug.Log("Start Queue SendHttp " + methodName); string url; if (methodName.Contains("http")) { url =methodName; } else { if(UserInfo.Instance.is20) { url = HttpAction.Instance.baseurl20 + methodName; } else { url = HttpAction.Instance.baseurl + methodName; } } Debug.Log(url+"开始通讯 " + methodName + " 发送数据 " + jsonString + " 是否Post" + isPost); // Debug.Log("URL:" + url+" isPost "+ isPost); UnityWebRequest webRequest; if (!isPost) { webRequest = UnityWebRequest.Get(url); } else { webRequest = new UnityWebRequest(url, "POST"); } Debug.Log(url + "开始通讯 URL " + url); initHead(); using (webRequest) { if (!isPost) { webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); foreach (var v in requestHeader) { webRequest.SetRequestHeader(v.Key, v.Value); } } else { 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.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)// { Debug.LogError(url + "\n" + webRequest.error + "\n" + webRequest.downloadHandler.text); string error = webRequest.downloadHandler.error; //JObject jObject = JObject.Parse(error); CallBack(error); } else { //Debug.Log(webRequest.downloadHandler.text); var mes = webRequest.downloadHandler.text; CallBack(mes); Debug.Log(url+"通讯回调信息 " + webRequest.downloadHandler.text); } } } 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; } }