HttpToolLangChao.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using login;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using UnityEngine;
  8. using UnityEngine.Networking;
  9. using XRTool.Util;
  10. public class HttpToolLangChao : MonoSingleton<HttpToolLangChao>
  11. {
  12. private Dictionary<string, string> requestHeader = new Dictionary<string, string>();
  13. public Dictionary<string, string> RequestHeader => requestHeader;
  14. public void PostTest(string methodName, string jsonString, Action<string> CallBack)
  15. {
  16. StartCoroutine(SendHttp(methodName, jsonString, CallBack));
  17. }
  18. public void Post(string methodName, string jsonString, Action<string> CallBack)
  19. {
  20. StartCoroutine(SendHttp(methodName, jsonString, CallBack));
  21. }
  22. public void PostLogin(string methodName, string jsonString, Action<string> CallBack)
  23. {
  24. StartCoroutine(SendHttp(methodName, jsonString, CallBack));
  25. }
  26. public void initHead(string ContentType)
  27. {
  28. requestHeader.Clear();
  29. if (Singleton<RTCInfo>.Instance.Token != "" && Singleton<RTCInfo>.Instance.Token != null)
  30. {
  31. requestHeader.Add("x-token", Singleton<RTCInfo>.Instance.Token);
  32. //requestHeader.Add("x-token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjQ4NzQxMTY1MDIsImlhdCI6MTcyMDUxNjUwMiwidXNlciI6eyJpZCI6MTA5MiwiZW1haWwiOiIiLCJwaG9uZSI6InprZGVtbyIsIm5hbWUiOiJ6a2RlbW8iLCJyb2xlSWQiOjF9fQ.OZ2HDUp9F5S504cmCUh7Ai15sq_nQ5i7ZG_odxhqGGM");
  33. requestHeader.Add("authorization", Singleton<RTCInfo>.Instance.Token);
  34. }
  35. requestHeader.Add("Content-Type", ContentType);
  36. }
  37. public IEnumerator SendHttp(string methodName, string jsonString, Action<string> CallBack, string ContentType = "application/json", bool isPost = true)
  38. {
  39. Debug.Log("Start Queue SendHttp " + methodName);
  40. string text = (!methodName.Contains("http")) ? ("https://api-fat2.ghz-tech.com" + methodName) : methodName;
  41. Debug.Log("URL:" + text + " isPost " + isPost);
  42. UnityWebRequest webRequest = isPost ? new UnityWebRequest(text, "POST") : UnityWebRequest.Get(text);
  43. initHead(ContentType);
  44. using (webRequest)
  45. {
  46. if (!isPost)
  47. {
  48. webRequest.downloadHandler = new DownloadHandlerBuffer();
  49. foreach (KeyValuePair<string, string> item in requestHeader)
  50. {
  51. webRequest.SetRequestHeader(item.Key, item.Value);
  52. }
  53. }
  54. else
  55. {
  56. byte[] bytes = Encoding.UTF8.GetBytes(jsonString);
  57. webRequest.uploadHandler = new UploadHandlerRaw(bytes);
  58. webRequest.downloadHandler = new DownloadHandlerBuffer();
  59. }
  60. foreach (KeyValuePair<string, string> item2 in requestHeader)
  61. {
  62. webRequest.SetRequestHeader(item2.Key, item2.Value);
  63. }
  64. yield return webRequest.SendWebRequest();
  65. Debug.Log("CallBack==>" + webRequest.downloadHandler.text);
  66. if (webRequest.isHttpError || webRequest.isNetworkError)
  67. {
  68. string text2 = webRequest.downloadHandler.text;
  69. CallBack(text2);
  70. }
  71. else
  72. {
  73. string text3 = webRequest.downloadHandler.text;
  74. CallBack(text3);
  75. }
  76. }
  77. }
  78. public void HeadAddToken(string token)
  79. {
  80. requestHeader.Add("x-token", token);
  81. }
  82. public string GetMd5Hash(string strToEncrypt)
  83. {
  84. MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
  85. byte[] bytes = Encoding.ASCII.GetBytes(strToEncrypt);
  86. byte[] array = mD5CryptoServiceProvider.ComputeHash(bytes);
  87. StringBuilder stringBuilder = new StringBuilder();
  88. for (int i = 0; i < 10; i++)
  89. {
  90. stringBuilder.Append(array[i].ToString("x2"));
  91. }
  92. return stringBuilder.ToString().Substring(0, 10);
  93. }
  94. }