/*------------------------------------------------------------------------------*/ IEnumerator PostJsonRequest(string url, object data, WebResponseHandler <string> onResponse) { var jsonString = JsonUtility.ToJson(data); // var req = CreatePostJSONRequest(url, data); // var req = UnityWebRequest.Post(url, jsonString); // req.downloadHandler = new DownloadHandlerJson(); // req.method = "POST"; var req = UWebRequest.UWebRequestCreator.PostJson(url, jsonString, new DownloadHandlerJson()); // yield return(req.Send()); // if (req.isError) { //Handle Error Debug.LogFormat("request {0} error {1}", url, req.error); // yield break; } if (req.responseCode != 200) { Debug.LogFormat("request {0} web error {1}", url, req.responseCode); // yield break; } // var jsonStr = DownloadHandlerJson.GetContent(req); // var response = new UWebResponse <string>(url, req.responseCode, req.error, jsonStr); onResponse(response); Debug.LogFormat("Web request {0} succes.\nResponse: {1}", url, jsonStr.ToString()); // this.textResponse.text = jsonStr; // }
// IEnumerator PostRequest(string url, string json, UWebRequestCallback <string> callback) { var request = UWebRequestCreator.PostJson(url, json, new DownloadHandlerJson()); yield return(request.Send()); //Response string data = DownloadHandlerJson.GetContent(request); var response = new UWebResponse <string>(request.url, request.responseCode, request.error, data); callback(response); }
void WebExampleCallback <T>(UWebResponse <T> response) { if (response.isError || response.ResponseCode != 200) { //handle error msg Debug.LogFormat("{0} request error({1}): {2}", response.RequestURL, response.ResponseCode, response.ErrorMsg); return; } //handle response success SetResponseText((string)(object)response.GetResponseData()); string strJson = response.GetResponseData() as string; var dict = MiniJSON.Json.Deserialize(strJson) as Dictionary <string, object>; }
/*----------------------------------------------------------------------------------*/ private void onResponseRecevied <T>(UWebResponse <T> response) { // // Debug.LogFormat("Web request {0} succes.\nResponse: {1}", url, response.ToString()); if (response.isError) { Debug.LogFormat("request error {0}", response.ErrorMsg); return; } Debug.LogFormat("response {0} status code {1}", response.RequestURL, response.ResponseCode); if (response.ResponseCode != 200) { return; } string data = (string)(object)response.GetResponseData(); this.textResponse.text = data; }
IEnumerator PostFormRequest(string url, WebResponseHandler <string> onResponse) { WWWForm form = new WWWForm(); form.AddField("username", "Test001"); form.AddField("name", "Tester 001"); form.AddField("action", "PostRequest"); var req = UWebRequest.UWebRequestCreator.PostRequest(url, form, new DownloadHandlerBuffer()); //UnityWebRequest.Post(url, form); yield return(req.Send()); var strResponse = req.downloadHandler.text; var response = new UWebResponse <string>(url, req.responseCode, req.error, strResponse); Debug.LogFormat("Web request {0} succes.\nResponse: {1}", url, strResponse); onResponse(response); }
IEnumerator SendWebRequest(string url, string method) { //Create Request //UnityWebRequest.Get(url); var request = CreateGetRequest(url); //new UnityWebRequest(url, method); byte[] data = new byte[1]; //request.uploadHandler = new UploadHandlerRaw(data); //request.downloadHandler = new DownloadHandlerBuffer(); // Debug.Log("Request content: " + request.GetRequestHeader("Accept")); //request.uploadHandler.contentType); //request send yield return(request.Send()); //downloadhandler.text would change!!! //Web Response var uwResponse = new UWebResponse <string>(url, request.responseCode, request.error, request.downloadHandler.text); if (request.isError) { //Request Error Debug.Log("" + request.error); //Handle Error //show error msg to TextUI textResponse.text = request.error; } //Log response var response = ResponseToString(request.downloadHandler.data); Debug.Log("Web request succes.\n Response: " + response); //Handle Response //show response to TextUI this.textResponse.text = response; }