123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- 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>
- {
- 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 Dictionary<string, string> RequestHeader
- {
- get { return requestHeader; }
- }
- public void Get(string methodName, Action<string> callback)
- {
- StartCoroutine(SendHttp(methodName,"", callback,false, false));
- }
- public void GetPoint(string methodName, Action<string> callback,bool isCloud=false)
- {
- StartCoroutine(SendHttp(methodName,"", callback,false, isCloud));
- }
- public void GetPointFile(string methodName, string projectId, string fileType, Action<string> callback)
- {
- string url =HttpAction.UrlPointCloud + methodName;
- // 创建URL
- UriBuilder uriBuilder = new UriBuilder(url);
- // 添加查询参数
- uriBuilder.Query = "projectId=" + projectId + "&fileType=" + fileType;
- StartCoroutine(SendHttp(uriBuilder.Uri.ToString(),"", callback,false));
- }
- public void PostTest(string methodName, string jsonString, Action<string> CallBack)
- {
- StartCoroutine(SendHttp(methodName, jsonString, CallBack));
- }
- public void Post(string methodName, string jsonString, Action<string> CallBack)
- {
- StartCoroutine(SendHttp(methodName, jsonString, CallBack));
- }
- public void GetAllMaterials(string methodName, string jsonString, Action<String> CallBack)
- {
- StartCoroutine(SendHttp(methodName, "", CallBack,false));
- }
- /// <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(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<string> CallBack,bool isPost=true,bool isCloud=false)
- {
- Debug.Log("Start Queue SendHttp " + methodName);
- string url;
- if (methodName.Contains("http"))
- {
- url =methodName;
- }
- else
- {
- if(isCloud)
- {
- url = HttpAction.UrlPointCloud + methodName;
- }else
- {
- if(UserInfo.Instance.is20)
- {
- url = HttpAction.baseurl20 + methodName;
- }
- else
- {
- url = HttpAction.baseurl + methodName;
- }
- }
- }
- Debug.Log("URL:" + url+" isPost "+ isPost);
- UnityWebRequest webRequest;
- if (!isPost)
- {
- webRequest = UnityWebRequest.Get(url);
- }
- else
- {
- webRequest = new UnityWebRequest(url, "POST");
- }
- 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();
- Debug.Log("CallBack==>"+ webRequest.downloadHandler.text);
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- Debug.LogError(url + "\n" + 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;
- CallBack(mes);
- }
- }
- }
- 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;
- }
- }
|