/// <summary> /// Sends a GET request to the master server and returns the HTTP or Json result. /// </summary> /// <param name="methodName">Name of the method on the server to use.</param> /// <param name="methodType">The type of method</param> /// <returns></returns> private string SendGetRequest(string methodName, WebServiceMethodTypeEnum methodType, string queryString = "") { WebRequest request = WebRequest.Create(BuildURLString(methodType) + methodName + queryString); request.ContentType = "application/json; charset=utf-8"; request.Method = "GET"; using (WebResponse response = request.GetResponse()) { if (IsMasterServerAlive()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { return ReplaceJsonArraySpecialCharacters(reader.ReadToEnd()); } } else { return ""; } } }
/// <summary> /// Sends a POST request to the master server, including a Json object. /// Returns the web server's Json result. /// </summary> /// <param name="methodName">The name of the method to call on the server.</param> /// <param name="methodType">The type of method to use.</param> /// <param name="jsonObject">The Json object to send in the request.</param> /// <returns></returns> private string SendJsonRequest(string methodName, WebServiceMethodTypeEnum methodType, object jsonObject) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(BuildURLString(methodType) + methodName); request.ContentType = "application/json; charset=utf-8"; request.Method = "POST"; if (IsMasterServerAlive()) { using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(jsonObject); writer.Flush(); writer.Close(); } using (WebResponse response = request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { return ReplaceJsonArraySpecialCharacters(reader.ReadToEnd()); } } } else { return ""; } }
/// <summary> /// Builds the URL to the master server. /// </summary> /// <param name="methodType">The method type to use.</param> /// <returns></returns> private string BuildURLString(WebServiceMethodTypeEnum methodType) { return MasterServerConfiguration.MasterServerURL + "api/APIControllers/" + EnumerationHelper.GetEnumerationDescription(methodType) + "/"; }