/// <summary> /// 发送以Json对象为参数的请求 /// </summary> /// <param name="requestType"></param> /// <param name="requestURL"></param> /// <param name="successResponse"></param> /// <param name="errorResponse"></param> /// <param name="data"></param> /// <param name="timeOut"></param> /// <param name="showWaiting"></param> /// <param name="callback"></param> /// <param name="apiValue"></param> public void JsonDataRequest( HttpRequestType requestType, string requestURL, OnSuccess successResponse, OnError errorResponse, JsonData data = default, int timeOut = 10, bool showWaiting = true, OnSuccess callback = default, string apiValue = "") { HTTPJsonRequestInfo info = new HTTPJsonRequestInfo { requestType = requestType, requestURL = requestURL, successResponse = successResponse, errorResponse = errorResponse, data = data, timeOut = timeOut, showWaiting = showWaiting, callback = callback, headerAPI = apiValue }; if (showWaiting) { mRequestInfoList.Add(info); } else { mRequestInfosNoWaiting.Add(info); } }
private void AfterRequest(ref HTTPJsonRequestInfo info, ref string requestURL, ref JsonData data, ref UnityWebRequest engine, DateTime debugTime, ref string debug) { LogTimeUsed(ref debug, ref engine, debugTime); string headerAPI = engine.GetRequestHeader("api"); CheckRequestError(ref engine, ref debug, out int errorStatu); Responsed(errorStatu, ref engine, info.successResponse, info.callback, info.errorResponse, ref data, ref requestURL, ref debug); engine.Dispose(); RemoveHeaderAPI(ref headerAPI); }
private void BeforeRequest(ref HTTPJsonRequestInfo info, out string requestURL, out JsonData data, out UnityWebRequest engine, ref string debug) { data = info.data; requestURL = info.requestURL; HttpRequestType requestType = info.requestType; LogBeforeSend(ref debug, ref requestType, ref requestURL, ref data); engine = CreateWebReqeust(requestType, ref requestURL, ref data, info.timeOut); InitAndSetHeaderAPI(ref engine, info.headerAPI);//标记HTTP请求头部数据,区分发送同一个请求的不同业务 }
/// <summary> /// 发送无需阻塞界面的请求 /// </summary> /// <param name="info"></param> /// <returns></returns> private IEnumerator SendRequestNoWaiting(HTTPJsonRequestInfo info) { string debug = string.Empty; DateTime debugTime = DateTime.UtcNow; BeforeRequest(ref info, out string requestURL, out JsonData data, out UnityWebRequest engine, ref debug); mIsNetWorkingNoWaiting = true; yield return(engine.SendWebRequest()); mIsNetWorkingNoWaiting = false; mRequestInfosNoWaiting.RemoveAt(0); AfterRequest(ref info, ref requestURL, ref data, ref engine, debugTime, ref debug); }
private bool IsRequestValid(ref HTTPJsonRequestInfo info, ref List <HTTPJsonRequestInfo> list) { bool allowSend = true; switch (info.requestType) { case HttpRequestType.Post: allowSend = info.data != default; if (!allowSend) { Debug.LogError("使用Http的Post方式(JSON)请求服务器,表单数据不能为空!URL: ".Append(info.requestURL)); list?.RemoveAt(0); } else { } break; } return(allowSend); }
/// <summary> /// 以构建信息项的方式创建JSON参数的网络请求,并加入网络请求的队列(附带等待环节) /// </summary> /// <param name="info"></param> /// <returns></returns> public bool StartRequestWaiting(HTTPJsonRequestInfo info) { bool allowSend = !mIsNetWorking; if (allowSend) { allowSend = IsRequestValid(ref info, ref mRequestInfoList); if (allowSend) { IEnumerator enumerator = SendRequestWaiting(info); ComponentOwner.StartCoroutine(enumerator); } else { } } else { } return(allowSend); }
private bool ShouldRequestFromList(ref List <HTTPJsonRequestInfo> list, out HTTPJsonRequestInfo item) { bool result = list.Count > 0; item = default; if (result) { item = list[0]; result = item != default; if (!result) { list.RemoveAt(0);//移除无效的请求信息,有效的请求会在发送完成后移除 } else { } } else { } return(result); }