/// <summary> /// Runs the request. /// </summary> /// <param name="url"> The url to send a request to. </param> /// <param name="methodType"> A method type. </param> /// <param name="errorHandler"> A callback to handle errors. </param> /// <param name="callback"> A callback to handle a succesful request. Null by default. </param> /// <param name="query"> A set of query parameters. Null by default. </param> /// <param name="body"> A byte array. </param> /// <param name="contentType"> A content type of this request. If left the value null, the contentType will be "application/json". </param> public static void RunRequest(string url, HTTPMethod methodType, RequestErrorCallback errorHandler, RequestResultCallback callback = null, MoBackRequestParameters query = null, byte[] body = null, string contentType = null) { WebRequest request; if (query != null) { request = WebRequest.Create(url + query); } else { request = WebRequest.Create(url); } if (MoBack.MoBackAppSettings.loggingLevel >= MoBackAppSettings.LoggingLevel.VERBOSE) { Debug.Log(request.RequestUri.AbsoluteUri); } // Insert keys WebHeaderCollection collection = new WebHeaderCollection(); collection.Add("X-Moback-Application-Key", MoBack.MoBackAppSettings.ApplicationID); collection.Add("X-Moback-Environment-Key", MoBack.MoBackAppSettings.EnvironmentKey); // Set session token if there is one. if (!string.IsNullOrEmpty(MoBack.MoBackAppSettings.SessionToken)) { collection.Add("X-Moback-SessionToken-Key", MoBack.MoBackAppSettings.SessionToken); } request.Headers = collection; // Specify request as GET, POST, PUT, or DELETE request.Method = methodType.ToString(); if (string.IsNullOrEmpty(contentType)) { request.ContentType = "application/json"; } else { request.ContentType = contentType; } // Specify request content length request.ContentLength = body == null ? 0 : body.Length; string responseFromServer = String.Empty; #if UNITY_ANDROID && !UNITY_EDITOR ServicePointManager.ServerCertificateValidationCallback = SSLValidator.Validator; #elif UNITY_EDITOR_WIN || (UNITY_STANDALONE_WIN && !UNITY_EDITOR_OSX) ServicePointManager.ServerCertificateValidationCallback = Validator; #endif try { // Open a stream and send the request to the remote server Stream dataStream = null; if (body != null) { dataStream = request.GetRequestStream(); dataStream.Write(body, 0, body.Length); dataStream.Close(); } // Complete the request, wait for and accept any response WebResponse response = request.GetResponse(); // Process response if (MoBackAppSettings.loggingLevel >= MoBackAppSettings.LoggingLevel.VERBOSE) { Debug.Log(((HttpWebResponse)response).StatusDescription); } dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); responseFromServer = reader.ReadToEnd(); // Debug.Log("the json is "+responseFromServer.ToString()); // Close all streams reader.Close(); response.Close(); } catch (WebException webException) { HttpWebResponse errorResponse = webException.Response as HttpWebResponse; if (errorResponse != null) { if (MoBackAppSettings.loggingLevel >= MoBackAppSettings.LoggingLevel.WARNINGS) { Debug.LogWarning(string.Format("Network Request Error {0}: {1}.\nFull Exception: {2}", (int)errorResponse.StatusCode, errorResponse.StatusCode.ToString(), MoBackError.FormatException(webException))); } errorHandler(webException, webException.Status, errorResponse.StatusCode, webException.Message); } else { if (MoBackAppSettings.loggingLevel >= MoBackAppSettings.LoggingLevel.WARNINGS) { Debug.LogWarning(string.Format("Network Request Failed with message {0}.\nFull exception: {1}", webException.Message, MoBackError.FormatException(webException))); } errorHandler(webException, webException.Status, null, webException.Message); } return; } if (MoBackAppSettings.loggingLevel >= MoBackAppSettings.LoggingLevel.VERBOSE) { Debug.Log(responseFromServer); } if (callback != null) { SimpleJSONNode responseAsJSON = SimpleJSONNode.Parse(responseFromServer); callback(responseFromServer, responseAsJSON); } }