WebRequestManager.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using LitJson;
  2. using System;
  3. using System.Collections;
  4. using System.Net.Http;
  5. using System.Net.Http.Headers;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using UnityEngine;
  9. using UnityEngine.Networking;
  10. namespace SC.XR.Unity.Module_PlatformAccount
  11. {
  12. public class WebRequestManager : WebRequestMgr
  13. {
  14. public override void PostData(string url, string headerJson, string formJson, Action<string> callback = null, Action<string> error = null)
  15. {
  16. JsonData header = JsonMapper.ToObject(headerJson);
  17. JsonData form = JsonMapper.ToObject(formJson);
  18. RequestData data = new RequestData();
  19. data.enumerator = PostsDataQueue(url, header, form, callback, error);
  20. RequsetQueue.Enqueue(data);
  21. }
  22. public override void PostData(string url, JsonData header, JsonData form, Action<string> callback = null, Action<string> error = null)
  23. {
  24. RequestData data = new RequestData();
  25. data.enumerator = PostsDataQueue(url, header, form, callback, error);
  26. RequsetQueue.Enqueue(data);
  27. }
  28. public override void PostImageData(string url, Texture2D userIcon, HttpClient client, string channel, string filename)
  29. {
  30. RequestData data = new RequestData();
  31. data.task = PostJsonData(url, userIcon, client, channel, filename);
  32. RequsetQueue.Enqueue(data);
  33. }
  34. public override void GetData(string url, string headerJson, string formJson, Action<string> callback = null, Action<string> error = null)
  35. {
  36. JsonData header = JsonMapper.ToObject(headerJson);
  37. JsonData form = JsonMapper.ToObject(formJson);
  38. RequestData data = new RequestData();
  39. data.enumerator = GetsDataQueue(url, header, form, callback, error);
  40. RequsetQueue.Enqueue(data);
  41. }
  42. public override void GetData(string url, JsonData header, JsonData form, Action<string> callback = null, Action<string> error = null)
  43. {
  44. RequestData data = new RequestData();
  45. data.enumerator = GetsDataQueue(url, header, form, callback, error);
  46. RequsetQueue.Enqueue(data);
  47. }
  48. public override IEnumerator GetsDataQueue(string url, JsonData header, JsonData form, Action<string> callback = null,Action<string> error = null)
  49. {
  50. using (UnityWebRequest web = UnityWebRequest.Get(url))
  51. {
  52. foreach (var key in header.Keys)
  53. {
  54. web.SetRequestHeader(key, (string)header[key]);
  55. }
  56. //把客户端数据传往服务器:给服务器上传我们的数据;
  57. web.uploadHandler = new UploadHandlerRaw(Encoding.Default.GetBytes(form != null ? JsonMapper.ToJson(form) : "null"));
  58. //服务器数据---》客户端 获取服务器返回的数据
  59. web.downloadHandler = new DownloadHandlerBuffer();
  60. PreSendWebRequestEvent?.Invoke(url, web);
  61. //发送一个请求:SendWebRequest异步,你发完后,不用等底层完整的发出去,你就可以做别的;防止卡主游戏线程
  62. yield return web.SendWebRequest();
  63. WebRequestCallBackEvent?.Invoke(url, web);
  64. IsRequesting = false;
  65. if (web.isNetworkError || web.isHttpError)
  66. {
  67. Debug.LogWarning("RequestData : " + url + web.error);
  68. error?.Invoke(web.error);
  69. yield break;
  70. }
  71. else
  72. {
  73. try
  74. {
  75. Debug.Log("RequestData : " + web.downloadHandler.text);
  76. callback?.Invoke(web.downloadHandler.text);
  77. }
  78. catch (Exception e)
  79. {
  80. Debug.Log("Exception message : " + e.Message);
  81. error?.Invoke(e.Message);
  82. }
  83. }
  84. }
  85. }
  86. public override IEnumerator PostsDataQueue(string url, JsonData header, JsonData form, Action<string> callback = null,Action<string> error = null)
  87. {
  88. using (UnityWebRequest web = UnityWebRequest.PostWwwForm(url, ""))
  89. {
  90. foreach (var key in header.Keys)
  91. {
  92. web.SetRequestHeader(key, (string)header[key]);
  93. }
  94. //把客户端数据传往服务器:给服务器上传我们的数据;
  95. web.uploadHandler = new UploadHandlerRaw(Encoding.Default.GetBytes(form != null ? JsonMapper.ToJson(form) : "null"));
  96. //服务器数据---》客户端 获取服务器返回的数据
  97. web.downloadHandler = new DownloadHandlerBuffer();
  98. PreSendWebRequestEvent?.Invoke(url, web);
  99. //发送一个请求:SendWebRequest异步,你发完后,不用等底层完整的发出去,你就可以做别的;防止卡主游戏线程
  100. yield return web.SendWebRequest();
  101. WebRequestCallBackEvent?.Invoke(url, web);
  102. IsRequesting = false;
  103. if (web.isNetworkError || web.isHttpError)
  104. {
  105. Debug.LogWarning("RequestData : " + url + web.error);
  106. error?.Invoke(web.error);
  107. yield break;
  108. }
  109. else
  110. {
  111. try
  112. {
  113. Debug.Log("RequestData : " + web.downloadHandler.text);
  114. callback?.Invoke(web.downloadHandler.text);
  115. }
  116. catch (Exception e)
  117. {
  118. Debug.Log("Exception message : " + e.Message);
  119. error?.Invoke(e.Message);
  120. }
  121. }
  122. }
  123. }
  124. public async Task PostJsonData(string url, Texture2D userIcon, HttpClient client, string channel, string filename)
  125. {
  126. string str = "";
  127. string boundary = string.Format("--{0}", DateTime.Now.Ticks.ToString("x"));
  128. var postContent = new MultipartFormDataContent();
  129. postContent.Headers.Add("ContentType", $"multipart/form-data, boundary={boundary}");
  130. postContent.Headers.Add("Channel", channel);
  131. client.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(LoginMgr.Instance.userData.token);
  132. string filekeyname = "file";
  133. postContent.Add(new ByteArrayContent(userIcon.EncodeToPNG()), filekeyname, filename);
  134. var response = await client.PostAsync(url, postContent);
  135. var ReadAsString = await response.Content.ReadAsStringAsync();
  136. IsRequesting = false;
  137. try
  138. {
  139. str = ReadAsString;
  140. Debug.Log("RequestData : " + str);
  141. }
  142. catch (Exception e)
  143. {
  144. Debug.Log("Exception message : " + e.Message);
  145. }
  146. }
  147. }
  148. }