/// <summary> /// Posts data to the HTTP server /// </summary> /// <param name="url"></param> /// <param name="data"></param> /// <returns></returns> IEnumerator PostData(string url, string data, HTTPRequestCallback callback) { var request = new UnityWebRequest(url, "POST"); byte[] bodyRaw = new System.Text.UTF8Encoding().GetBytes(data); request.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw); request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer(); request.SetRequestHeader("Content-Type", "application/json"); yield return(request.SendWebRequest()); //messageText.text = request.error; if (callback != null) { callback(request.downloadHandler.text, false); } }
/// <summary> /// Sends out an async get request using the Unity networking module /// </summary> /// <param name="uri"></param> /// <param name="getCallback"></param> /// <returns></returns> IEnumerator GetRequest(string uri, HTTPRequestCallback getCallback) { using (UnityWebRequest webRequest = UnityWebRequest.Get(uri)) { // Request and wait for the desired page. yield return(webRequest.SendWebRequest()); // string[] pages = uri.Split('/'); //int page = pages.Length - 1; if (webRequest.isNetworkError) { getCallback(webRequest.downloadHandler.text, true); // ALWAYS call the callback! } else { getCallback(webRequest.downloadHandler.text, false); // ALWAYS call the callback! } } }
public YRETCODE HTTPRequestAsync(string request, HTTPRequestCallback callback, blockingCallbackCtx context, ref string errmsg) { YRETCODE res = default(YRETCODE); StringBuilder errbuf = new StringBuilder(YAPI.YOCTO_ERRMSG_LEN); StringBuilder b = null; int neededsize = 0; int p = 0; string newrequest = null; int written = 0; StringBuilder root = new StringBuilder(YAPI.YOCTO_SERIAL_LEN); int tmp = 0; if (((_flags & YAPI.HTTP_ASYNC_PENDING) != 0)) HTTPRequestAsyncFinish(true); _flags = _flags | YAPI.HTTP_ASYNC_PENDING; _http_result = ""; _context = context; _callback = callback; _cacheStamp = YAPI.GetTickCount(); // invalidate cache if (!(_subpathinit)) { res = YAPI._yapiGetDevicePath(_devdescr, root, null, 0, ref neededsize, errbuf); if (YAPI.YISERR(res)) { _flags = _flags & ~YAPI.HTTP_ASYNC_PENDING; errmsg = errbuf.ToString(); return res; } b = new StringBuilder(neededsize); res = YAPI._yapiGetDevicePath(_devdescr, root, b, neededsize, ref tmp, errbuf); if (YAPI.YISERR(res)) { _flags = _flags & ~YAPI.HTTP_ASYNC_PENDING; errmsg = errbuf.ToString(); return res; } _rootdevice = root.ToString(); _subpath = b.ToString(); _subpathinit = true; } p = request.IndexOf("/"); newrequest = request.Substring(0, p) + _subpath + request.Substring(p + 1, request.Length - p - 1); res = YAPI._yapiRequestOpen(ref _iohdl, new StringBuilder(_rootdevice), errbuf); if (YAPI.YISERR(res)) { _flags = _flags & ~YAPI.HTTP_ASYNC_PENDING; errmsg = errbuf.ToString(); return res; } written = YAPI._yapiRequestWrite(ref _iohdl, new StringBuilder(newrequest), newrequest.Length, errbuf); if (YAPI.YISERR(written)) { errmsg = errbuf.ToString(); YAPI._yapiRequestClose(ref _iohdl, null); _flags = _flags & ~YAPI.HTTP_ASYNC_PENDING; return written; } return YAPI.SUCCESS; }