123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- using LitJson;
- using System;
- using System.Collections;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Threading.Tasks;
- using UnityEngine;
- using UnityEngine.Networking;
- namespace SC.XR.Unity.Module_PlatformAccount
- {
- public class WebRequestManager : WebRequestMgr
- {
- public override void PostData(string url, string headerJson, string formJson, Action<string> callback = null, Action<string> error = null)
- {
- JsonData header = JsonMapper.ToObject(headerJson);
- JsonData form = JsonMapper.ToObject(formJson);
- RequestData data = new RequestData();
- data.enumerator = PostsDataQueue(url, header, form, callback, error);
- RequsetQueue.Enqueue(data);
- }
- public override void PostData(string url, JsonData header, JsonData form, Action<string> callback = null, Action<string> error = null)
- {
- RequestData data = new RequestData();
- data.enumerator = PostsDataQueue(url, header, form, callback, error);
- RequsetQueue.Enqueue(data);
- }
- public override void PostImageData(string url, Texture2D userIcon, HttpClient client, string channel, string filename)
- {
- RequestData data = new RequestData();
- data.task = PostJsonData(url, userIcon, client, channel, filename);
- RequsetQueue.Enqueue(data);
- }
- public override void GetData(string url, string headerJson, string formJson, Action<string> callback = null, Action<string> error = null)
- {
- JsonData header = JsonMapper.ToObject(headerJson);
- JsonData form = JsonMapper.ToObject(formJson);
- RequestData data = new RequestData();
- data.enumerator = GetsDataQueue(url, header, form, callback, error);
- RequsetQueue.Enqueue(data);
- }
- public override void GetData(string url, JsonData header, JsonData form, Action<string> callback = null, Action<string> error = null)
- {
- RequestData data = new RequestData();
- data.enumerator = GetsDataQueue(url, header, form, callback, error);
- RequsetQueue.Enqueue(data);
- }
- public override IEnumerator GetsDataQueue(string url, JsonData header, JsonData form, Action<string> callback = null,Action<string> error = null)
- {
- using (UnityWebRequest web = UnityWebRequest.Get(url))
- {
- foreach (var key in header.Keys)
- {
- web.SetRequestHeader(key, (string)header[key]);
- }
- //把客户端数据传往服务器:给服务器上传我们的数据;
- web.uploadHandler = new UploadHandlerRaw(Encoding.Default.GetBytes(form != null ? JsonMapper.ToJson(form) : "null"));
- //服务器数据---》客户端 获取服务器返回的数据
- web.downloadHandler = new DownloadHandlerBuffer();
- PreSendWebRequestEvent?.Invoke(url, web);
- //发送一个请求:SendWebRequest异步,你发完后,不用等底层完整的发出去,你就可以做别的;防止卡主游戏线程
- yield return web.SendWebRequest();
- WebRequestCallBackEvent?.Invoke(url, web);
- IsRequesting = false;
- if (web.isNetworkError || web.isHttpError)
- {
- Debug.LogWarning("RequestData : " + url + web.error);
- error?.Invoke(web.error);
- yield break;
- }
- else
- {
- try
- {
- Debug.Log("RequestData : " + web.downloadHandler.text);
- callback?.Invoke(web.downloadHandler.text);
- }
- catch (Exception e)
- {
- Debug.Log("Exception message : " + e.Message);
- error?.Invoke(e.Message);
- }
- }
- }
- }
- public override IEnumerator PostsDataQueue(string url, JsonData header, JsonData form, Action<string> callback = null,Action<string> error = null)
- {
- using (UnityWebRequest web = UnityWebRequest.PostWwwForm(url, ""))
- {
- foreach (var key in header.Keys)
- {
- web.SetRequestHeader(key, (string)header[key]);
- }
- //把客户端数据传往服务器:给服务器上传我们的数据;
- web.uploadHandler = new UploadHandlerRaw(Encoding.Default.GetBytes(form != null ? JsonMapper.ToJson(form) : "null"));
- //服务器数据---》客户端 获取服务器返回的数据
- web.downloadHandler = new DownloadHandlerBuffer();
- PreSendWebRequestEvent?.Invoke(url, web);
- //发送一个请求:SendWebRequest异步,你发完后,不用等底层完整的发出去,你就可以做别的;防止卡主游戏线程
- yield return web.SendWebRequest();
- WebRequestCallBackEvent?.Invoke(url, web);
- IsRequesting = false;
- if (web.isNetworkError || web.isHttpError)
- {
- Debug.LogWarning("RequestData : " + url + web.error);
- error?.Invoke(web.error);
- yield break;
- }
- else
- {
- try
- {
- Debug.Log("RequestData : " + web.downloadHandler.text);
- callback?.Invoke(web.downloadHandler.text);
- }
- catch (Exception e)
- {
- Debug.Log("Exception message : " + e.Message);
- error?.Invoke(e.Message);
- }
- }
- }
- }
- public async Task PostJsonData(string url, Texture2D userIcon, HttpClient client, string channel, string filename)
- {
- string str = "";
- string boundary = string.Format("--{0}", DateTime.Now.Ticks.ToString("x"));
- var postContent = new MultipartFormDataContent();
- postContent.Headers.Add("ContentType", $"multipart/form-data, boundary={boundary}");
- postContent.Headers.Add("Channel", channel);
- client.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(LoginMgr.Instance.userData.token);
- string filekeyname = "file";
- postContent.Add(new ByteArrayContent(userIcon.EncodeToPNG()), filekeyname, filename);
- var response = await client.PostAsync(url, postContent);
- var ReadAsString = await response.Content.ReadAsStringAsync();
- IsRequesting = false;
- try
- {
- str = ReadAsString;
- Debug.Log("RequestData : " + str);
- }
- catch (Exception e)
- {
- Debug.Log("Exception message : " + e.Message);
- }
- }
- }
- }
|