123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- 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<HttpToolLangChao>
- {
- private Dictionary<string, string> requestHeader = new Dictionary<string, string>();
- public Dictionary<string, string> RequestHeader => requestHeader;
-
- 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 PostLogin(string methodName, string jsonString, Action<string> CallBack)
- {
- StartCoroutine(SendHttp(methodName, jsonString, CallBack));
- }
- public void initHead(string ContentType)
- {
- requestHeader.Clear();
- if (Singleton<RTCInfo>.Instance.Token != "" && Singleton<RTCInfo>.Instance.Token != null)
- {
- requestHeader.Add("x-token", Singleton<RTCInfo>.Instance.Token);
- //requestHeader.Add("x-token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjQ4NzQxMTY1MDIsImlhdCI6MTcyMDUxNjUwMiwidXNlciI6eyJpZCI6MTA5MiwiZW1haWwiOiIiLCJwaG9uZSI6InprZGVtbyIsIm5hbWUiOiJ6a2RlbW8iLCJyb2xlSWQiOjF9fQ.OZ2HDUp9F5S504cmCUh7Ai15sq_nQ5i7ZG_odxhqGGM");
- requestHeader.Add("authorization", Singleton<RTCInfo>.Instance.Token);
- }
- requestHeader.Add("Content-Type", ContentType);
- }
- public IEnumerator SendHttp(string methodName, string jsonString, Action<string> 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<string, string> 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<string, string> 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);
- }
- }
|