HttpTool.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using UnityEngine.Networking;
  6. using System.Text;
  7. using System.Security.Cryptography;
  8. using System.Net;
  9. using System.IO;
  10. using Newtonsoft.Json.Linq;
  11. using login;
  12. /// <summary>
  13. /// Http Request SDK
  14. /// </summary>
  15. public class HttpTool : MonoSingleton<HttpTool>
  16. {
  17. Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
  18. void Awake()
  19. {
  20. //http header 的内容
  21. // requestHeader.Add("Content-Type", "application/json");
  22. }
  23. // public string Token
  24. // {
  25. // get { return token; }
  26. // }
  27. public Dictionary<string, string> RequestHeader
  28. {
  29. get { return requestHeader; }
  30. }
  31. public void PostTest(string methodName, string jsonString, Action<string> CallBack)
  32. {
  33. StartCoroutine(SendHttp(methodName, jsonString, CallBack));
  34. }
  35. public void Post(string methodName, string jsonString, Action<string> CallBack)
  36. {
  37. StartCoroutine(SendHttp(methodName, jsonString, CallBack));
  38. }
  39. /// <summary>
  40. /// 登录
  41. /// </summary>
  42. /// <param name="methodName"></param>
  43. /// <param name="jsonString"></param>
  44. /// <param name="CallBack"></param>
  45. public void PostLogin(string methodName, string jsonString, Action<string> CallBack)
  46. {
  47. StartCoroutine(SendHttp(methodName, jsonString, CallBack));
  48. }
  49. public void initHead(string ContentType)
  50. {
  51. requestHeader.Clear();
  52. if (login.UserInfo.Instance.Token != "" && login.UserInfo.Instance.Token != null)
  53. {
  54. requestHeader.Add("x-token", login.UserInfo.Instance.Token);
  55. requestHeader.Add("authorization", login.UserInfo.Instance.Token);
  56. }
  57. requestHeader.Add("Content-Type", ContentType);
  58. }
  59. public IEnumerator SendHttp(string methodName, string jsonString, Action<string> CallBack, string ContentType = "application/json", bool isPost = true)
  60. {
  61. Debug.Log("Start Queue SendHttp " + methodName);
  62. string url;
  63. if (methodName.Contains("http"))
  64. {
  65. url = methodName;
  66. }
  67. else
  68. {
  69. url = HttpAction.Instance.baseurl + methodName;
  70. }
  71. Debug.Log("URL:" + url + " isPost " + isPost);
  72. UnityWebRequest webRequest;
  73. if (!isPost)
  74. {
  75. webRequest = UnityWebRequest.Get(url);
  76. }
  77. else
  78. {
  79. webRequest = new UnityWebRequest(url, "POST");
  80. }
  81. initHead(ContentType);
  82. using (webRequest)
  83. {
  84. if (!isPost)
  85. {
  86. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  87. foreach (var v in requestHeader)
  88. {
  89. webRequest.SetRequestHeader(v.Key, v.Value);
  90. }
  91. }
  92. else
  93. {
  94. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  95. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  96. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  97. }
  98. foreach (var v in requestHeader)
  99. {
  100. webRequest.SetRequestHeader(v.Key, v.Value);
  101. }
  102. yield return webRequest.SendWebRequest();
  103. Debug.Log("CallBack==>" + webRequest.downloadHandler.text);
  104. if (webRequest.isHttpError || webRequest.isNetworkError)
  105. {
  106. string error = webRequest.downloadHandler.text;
  107. CallBack(error);
  108. }
  109. else
  110. {
  111. //Debug.Log(webRequest.downloadHandler.text);
  112. var mes = webRequest.downloadHandler.text;
  113. CallBack(mes);
  114. }
  115. }
  116. }
  117. public void HeadAddToken(string token)
  118. {
  119. requestHeader.Add("x-token", token);
  120. }
  121. public string GetMd5Hash(string strToEncrypt)
  122. {
  123. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  124. byte[] bytes = Encoding.ASCII.GetBytes(strToEncrypt);
  125. byte[] encoded = md5.ComputeHash(bytes);
  126. StringBuilder sb = new StringBuilder();
  127. for (int i = 0; i < 10; i++)
  128. {
  129. sb.Append(encoded[i].ToString("x2"));
  130. }
  131. string password = sb.ToString();
  132. password = password.Substring(0, 10);
  133. return password;
  134. }
  135. }