HttpTool.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. /// <summary>
  12. /// Http Request SDK
  13. /// </summary>
  14. public class HttpTool : MonoSingleton<HttpTool>
  15. {
  16. Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
  17. void Awake()
  18. {
  19. //http header 的内容
  20. requestHeader.Add("Content-Type", "application/json");
  21. }
  22. // public string Token
  23. // {
  24. // get { return token; }
  25. // }
  26. public Dictionary<string, string> RequestHeader
  27. {
  28. get { return requestHeader; }
  29. }
  30. public void Get(string methodName, Action<string> callback)
  31. {
  32. StartCoroutine(SendHttp(methodName,"", callback,false));
  33. }
  34. public void PostTest(string methodName, string jsonString, Action<string> CallBack)
  35. {
  36. StartCoroutine(SendHttp(methodName, jsonString, CallBack));
  37. }
  38. public void Post(string methodName, string jsonString, Action<string> CallBack)
  39. {
  40. StartCoroutine(SendHttp(methodName, jsonString, CallBack));
  41. }
  42. public void GetAllMaterials(string methodName, string jsonString, Action<String> CallBack)
  43. {
  44. StartCoroutine(SendHttp(methodName, "", CallBack,false));
  45. }
  46. /// <summary>
  47. /// 登录
  48. /// </summary>
  49. /// <param name="methodName"></param>
  50. /// <param name="jsonString"></param>
  51. /// <param name="CallBack"></param>
  52. public void PostLogin(string methodName, string jsonString, Action<string> CallBack)
  53. {
  54. StartCoroutine(SendHttp(methodName, jsonString, CallBack));
  55. }
  56. public void initHead()
  57. {
  58. requestHeader.Clear();
  59. if (UserInfo.Instance.Token!="" && UserInfo.Instance.Token!=null)
  60. {
  61. requestHeader.Add("x-token", UserInfo.Instance.Token);
  62. requestHeader.Add("authorization", UserInfo.Instance.Token);
  63. }
  64. requestHeader.Add("Content-Type", "application/json");
  65. }
  66. public IEnumerator SendHttp(string methodName, string jsonString, Action<string> CallBack,bool isPost=true)
  67. {
  68. //Debug.Log("Start Queue SendHttp " + methodName);
  69. string url;
  70. if (methodName.Contains("http"))
  71. {
  72. url =methodName;
  73. }
  74. else
  75. {
  76. if(UserInfo.Instance.is20)
  77. {
  78. url = HttpAction.Instance.baseurl20 + methodName;
  79. }
  80. else
  81. {
  82. url = HttpAction.Instance.baseurl + methodName;
  83. }
  84. }
  85. Debug.Log(url+"开始通讯 " + methodName + " 发送数据 " + jsonString + " 是否Post" + isPost);
  86. // Debug.Log("URL:" + url+" isPost "+ isPost);
  87. UnityWebRequest webRequest;
  88. if (!isPost)
  89. {
  90. webRequest = UnityWebRequest.Get(url);
  91. }
  92. else
  93. {
  94. webRequest = new UnityWebRequest(url, "POST");
  95. }
  96. Debug.Log(url + "开始通讯 URL " + url);
  97. initHead();
  98. using (webRequest)
  99. {
  100. if (!isPost)
  101. {
  102. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  103. foreach (var v in requestHeader)
  104. {
  105. webRequest.SetRequestHeader(v.Key, v.Value);
  106. }
  107. }
  108. else
  109. {
  110. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  111. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  112. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  113. }
  114. foreach (var v in requestHeader)
  115. {
  116. webRequest.SetRequestHeader(v.Key, v.Value);
  117. }
  118. yield return webRequest.SendWebRequest();
  119. if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)//
  120. {
  121. Debug.LogError(url + "\n" + webRequest.error + "\n" + webRequest.downloadHandler.text);
  122. string error = webRequest.downloadHandler.error;
  123. //JObject jObject = JObject.Parse(error);
  124. CallBack(error);
  125. }
  126. else
  127. {
  128. //Debug.Log(webRequest.downloadHandler.text);
  129. var mes = webRequest.downloadHandler.text;
  130. CallBack(mes);
  131. Debug.Log(url+"通讯回调信息 " + webRequest.downloadHandler.text);
  132. }
  133. }
  134. }
  135. public void HeadAddToken(string token)
  136. {
  137. requestHeader.Add("x-token", token);
  138. }
  139. public string GetMd5Hash(string strToEncrypt)
  140. {
  141. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  142. byte[] bytes = Encoding.ASCII.GetBytes(strToEncrypt);
  143. byte[] encoded = md5.ComputeHash(bytes);
  144. StringBuilder sb = new StringBuilder();
  145. for (int i = 0; i < 10; i++)
  146. {
  147. sb.Append(encoded[i].ToString("x2"));
  148. }
  149. string password = sb.ToString();
  150. password = password.Substring(0, 10);
  151. return password;
  152. }
  153. }