/// <summary> /// Executes a web request against the OpSkins API. /// </summary> /// <param name="url">A string containing the URL for the web request.</param> /// <param name="method">A string indicating the method of the web request.</param> /// <param name="authToken">A string containing the OpSkins authentication token to use for the request.</param> /// <param name="data">A string containing data relating to the web request.</param> /// <returns>A string containing the result returned by the web request.</returns> internal static string ExecuteRequest(string url, string method, string authToken, string data = null) { HttpWebRequest Request; StreamReader Reader = null; HttpWebResponse Response; string ReturnValue = null; // Configure the request Request = (HttpWebRequest)HttpWebRequest.Create(url); Request.Headers.Add("authorization", "Basic " + authToken); Request.Method = method; // Add post data if (method == "POST" && data != null && data.Length > 0) { byte[] Data; Stream RequestStream; Data = System.Text.Encoding.ASCII.GetBytes(data); Request.ContentType = "application/x-www-form-urlencoded"; Request.ContentLength = Data.Length; RequestStream = Request.GetRequestStream(); RequestStream.Write(Data, 0, Data.Length); RequestStream.Flush(); RequestStream.Close(); } try { Response = (HttpWebResponse)Request.GetResponse(); try { Reader = new StreamReader(Response.GetResponseStream()); ReturnValue = Reader.ReadToEnd(); } finally { if (Reader != null) { Reader.Close(); } } } catch (WebException wex) { if (wex.Response != null) { try { string WexData; StreamReader WexReader; WexReader = new StreamReader(wex.Response.GetResponseStream()); WexData = WexReader.ReadToEnd(); ReturnValue = WexData; // _LogWriter.LogMessage(LogFileEntryPriority.Error, "ExecuteRequest", "WebException error '{0}' generated with response: {1}", wex.Message, ReturnValue); } catch (Exception) { // _LogWriter.LogMessage(LogFileEntryPriority.Error, "ExecuteRequest", "WebException error '{0}' generated. Unable to process Response object with exception '{1}'", wex.Message, ex.Message); } } else { //_LogWriter.LogMessage(LogFileEntryPriority.Error, "ExecuteRequest", "WebException error '{0}' generated without Response object", wex.Message); } } catch (Exception) { //_LogWriter.LogMessage(LogFileEntryPriority.Error, "ExecuteRequest", "Exception error '{0}' generated", ex.Message); } return(ReturnValue); }