unity之通用Http请求(Get\Post)
逻辑结构图:
TestHttpCallBackArgs http回调参数
1 using System; 2 3 ///4 /// http回调参数 5 /// 6 public class TestHttpCallBackArgs : EventArgs 7 { 8 /// 9 /// 是否有错 10 /// 11 public bool HasError; 12 /// 13 /// 错误值 14 /// 15 public string ErrorValue; 16 /// 17 /// 值 18 /// 19 public string Value; 20 /// 21 /// 数据 22 /// 23 public byte[] Data; 24 25 public void Init() 26 { 27 HasError = false; 28 ErrorValue = string.Empty; 29 Value = string.Empty; 30 Data = null; 31 } 32 }
TestHttpRoutine http访问器
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.Networking; 5 6 public delegate void HttpSendDataCallBack(TestHttpCallBackArgs callBackArgs); 7 8 ///9 /// http访问器 10 /// 11 public class TestHttpRoutine 12 { 13 private HttpSendDataCallBack m_SendCallBack; 14 private readonly TestHttpCallBackArgs m_CallBackArgs; 15 private bool m_IsBusy; 16 /// 17 /// 是否获取Data数据 18 /// 19 private bool m_IsGetData; 20 21 public TestHttpRoutine() 22 { 23 m_CallBackArgs = TestGameEntry.PoolMgr.Class_Dequeue (); 24 m_CallBackArgs.Init(); 25 } 26 27 /// 28 /// 发送数据 29 /// 30 /// 31 /// 32 /// 33 /// 34 /// 35 public void SendData(string url, HttpSendDataCallBack sendCallBack, bool isPost = false, bool isGetData = false, Dictionary<object, object> data = null) 36 { 37 if (m_IsBusy) return; 38 m_IsBusy = true; 39 m_IsGetData = isGetData; 40 m_SendCallBack = sendCallBack; 41 string json = string.Empty; 42 if (isPost) 43 { 44 json = LitJson.JsonMapper.ToJson(data); 45 PostUrl(url, json); 46 } 47 else 48 { 49 GetUrl(url); 50 } 51 if (!m_IsGetData) 52 { 53 Debug.Log(" 发送消息: " + url + ""); 54 if (json != string.Empty) 55 { 56 Debug.Log(" ==>> " + json + ""); 57 } 58 } 59 } 60 61 ///62 /// Get请求 63 /// 64 /// 65 private void GetUrl(string url) 66 { 67 UnityWebRequest webRequest = UnityWebRequest.Get(url); 68 TestGameEntry.Instance.StartCoroutine(Request(webRequest)); 69 } 70 71 /// 72 /// post请求 73 /// 74 /// 75 /// 76 private void PostUrl(string url, string json) 77 { 78 WWWForm form = new WWWForm(); 79 form.AddField("json", json); 80 UnityWebRequest webRequest = UnityWebRequest.Post(url, form); 81 TestGameEntry.Instance.StartCoroutine(Request(webRequest)); 82 } 83 84 private IEnumerator Request(UnityWebRequest webRequest) 85 { 86 yield return webRequest.SendWebRequest(); 87 m_IsBusy = false; 88 if (webRequest.result != UnityWebRequest.Result.Success) 89 { 90 m_CallBackArgs.HasError = true; 91 m_CallBackArgs.ErrorValue = webRequest.error; 92 m_SendCallBack?.Invoke(m_CallBackArgs); 93 if (!m_IsGetData) 94 { 95 Debug.Log(" 接收消息: " + webRequest.url + ""); 96 Debug.Log(" ==>> " + JsonUtility.ToJson(m_CallBackArgs) + ""); 97 } 98 } 99 else 100 { 101 m_CallBackArgs.HasError = false; 102 m_CallBackArgs.Value = webRequest.downloadHandler.text; 103 //webRequest.downloadHandler.data 数据是字节数组,序列化也序列化不出来内容 非GetData打印日志,并在data赋值之前打印 104 if (!m_IsGetData) 105 { 106 Debug.Log("接收消息: " + webRequest.url + ""); 107 Debug.Log(" ==>> " + JsonUtility.ToJson(m_CallBackArgs) + ""); 108 } 109 m_CallBackArgs.Data = webRequest.downloadHandler.data; 110 m_SendCallBack?.Invoke(m_CallBackArgs); 111 } 112 TestGameEntry.PoolMgr.Class_Equeue(m_CallBackArgs); 113 webRequest.Dispose(); 114 webRequest = null; 115 TestGameEntry.PoolMgr.Class_Equeue(this); 116 } 117 }