private static IEnumerator PostHttpRequestIEnumerator(string url, string data, PHPStringHandler phpHandler, bool post = true) { yield return(null); data = data == null ? string.Empty : data; Log("PHP 请求:" + url + data); UnityWebRequest uwr = null; if (post) { uwr = UnityWebRequest.Post(url, data); } else { uwr = UnityWebRequest.Get(url + data); } uwr.timeout = 15; if (!string.IsNullOrEmpty(data)) { byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(data); uwr.uploadHandler = (UploadHandler) new UploadHandlerRaw(postBytes); uwr.uploadHandler.contentType = "application/json"; } yield return(uwr.SendWebRequest()); string str = null; if (!string.IsNullOrEmpty(uwr.error)) { LogError(uwr.error); } else { str = uwr.downloadHandler.text; Utils.Log(str); } uwr.Abort(); if (phpHandler != null) { phpHandler(str); } }
public static void PostHttpRequest(string webUrl, string data, PHPStringHandler phpHandler, bool post = true) { StartDoCoroutine(PostHttpRequestIEnumerator(webUrl, data, phpHandler, post)); }