private ResponseData ManagedRequest(string json, string action, Func<string, string, NameValueCollection, ResponseData> requestMethod) { ResponseData response = new ResponseData(HttpStatusCode.BadRequest); // Attempt #1 with existing tokens if (_authTokens != null) { response = requestMethod(json, action, _authTokens); if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.BadRequest) { _authTokens = null; } else { return response; } } // Attempt #2 by logging in and obtaining fresh tokens if (_authTokens == null) { response = SignIn(); if (response.StatusCode == HttpStatusCode.OK) { response = requestMethod(json, action, _authTokens); } } return response; }
private ResponseData ManagedRequest(string json, string action, Func<string, string, NameValueCollection, ResponseData> requestMethod) { var commanderName = EDDiscoveryForm.EDDConfig.CurrentCommander.Name; JObject jo = JObject.Parse(json); jo["user"] = HttpUtility.UrlEncode(commanderName); json = jo.ToString(); ResponseData response = new ResponseData(HttpStatusCode.BadRequest); // Attempt #1 with existing tokens if (_authTokens != null) { response = requestMethod(json, action, _authTokens); if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.BadRequest) { _authTokens = null; } else { return response; } } // Attempt #2 by logging in and obtaining fresh tokens if (_authTokens == null) { response = SignIn(); if (response.StatusCode == HttpStatusCode.OK) { response = requestMethod(json, action, _authTokens); } } return response; }
private ResponseData ManagedRequest(string json, string action, Func <string, string, NameValueCollection, ResponseData> requestMethod) { var commanderName = EDDiscoveryForm.EDDConfig.CurrentCommander.Name; JObject jo = JObject.Parse(json); jo["user"] = HttpUtility.UrlEncode(commanderName); json = jo.ToString(); ResponseData response = new ResponseData(HttpStatusCode.BadRequest); // Attempt #1 with existing tokens if (_authTokens != null) { response = requestMethod(json, action, _authTokens); if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.BadRequest) { _authTokens = null; } else { return(response); } } // Attempt #2 by logging in and obtaining fresh tokens if (_authTokens == null) { response = SignIn(); if (response.StatusCode == HttpStatusCode.OK) { response = requestMethod(json, action, _authTokens); } } return(response); }
private ResponseData getResponseData(HttpWebResponse response, bool? error = null) { if (response == null) return new ResponseData(HttpStatusCode.NotFound); var dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); var data = new ResponseData(response.StatusCode, reader.ReadToEnd(), response.Headers); reader.Close(); dataStream.Close(); return data; }
private ResponseData SignIn() { _authTokens = null; var appSettings = ConfigurationManager.AppSettings; #if DEBUG // Testing db username/password is public so other contributors // can work with it var username = "******"; var password = "******"; #else // This is for the production database, so we're keeping the // credentials hidden away var username = appSettings["EDMaterializerUsername"]; var password = appSettings["EDMaterializerPassword"]; #endif ResponseData response = new ResponseData(HttpStatusCode.BadRequest); if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password)) { MessageBox.Show("Unabled to login to the EdMaterializer server, the credentials file is missing from the installation", "Unauthorized", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { var joSignIn = new JObject { { "email", username }, { "password", password } }; response = RequestPost(joSignIn.ToString(), $"{_authPath}/sign_in"); if (response.StatusCode == HttpStatusCode.OK) { var headers = response.Headers; var tokens = new NameValueCollection(); tokens["access-token"] = headers["access-token"]; tokens["client"] = headers["client"]; tokens["uid"] = headers["uid"]; ; _authTokens = tokens; } else { MessageBox.Show("Their was an error logging in to the EdMaterializer server.\nCheck the logs for details", "Unauthorized", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } return response; }
protected ResponseData RequestDelete(string action, NameValueCollection headers = null) { try { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_serverAddress + action); request.Method = "DELETE"; // Set the ContentType property of the WebRequest. request.ContentType = "application/json; charset=utf-8"; request.Headers.Add("Accept-Encoding", "gzip,deflate"); if (headers != null) { request.Headers.Add(headers); } request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog("DELETE " + request.RequestUri, ""); } // Get the response. //request.Timeout = 740 * 1000; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Display the status. // Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. Stream dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. var data = new ResponseData(response.StatusCode, reader.ReadToEnd(), response.Headers); // Display the content. // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog(data.Body, ""); } return(data); } catch (WebException ex) { using (WebResponse response = ex.Response) { HttpWebResponse httpResponse = (HttpWebResponse)response; System.Diagnostics.Trace.WriteLine(ex.StackTrace); System.Diagnostics.Trace.WriteLine("WebException : " + ex.Message); System.Diagnostics.Trace.WriteLine($"HTTP Error code: {httpResponse.StatusCode}"); System.Diagnostics.Trace.WriteLine(ex.StackTrace); if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog("WebException" + ex.Message, ""); WriteLog($"HTTP Error code: {httpResponse.StatusCode}", ""); } return(new ResponseData(httpResponse.StatusCode)); } } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine("Exception : " + ex.Message); System.Diagnostics.Trace.WriteLine(ex.StackTrace); if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog("Exception" + ex.Message, ""); } return(new ResponseData(HttpStatusCode.BadRequest)); } }
protected ResponseData RequestPost(string json, string action, NameValueCollection headers = null) { try { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_serverAddress + action); // Set the Method property of the request to POST. request.Method = "POST"; if (headers != null) { request.Headers.Add(headers); } // Create POST data and convert it to a byte array. //WRITE JSON DATA TO VARIABLE D //string postData = "{\"requests\":[{\"C\":\"Gpf_Auth_Service\", \"M\":\"authenticate\", \"fields\":[[\"name\",\"value\"],[\"Id\",\"\"],[\"username\",\"[email protected]\"],[\"password\",\"ab9ce908\"],[\"rememberMe\",\"Y\"],[\"language\",\"en-US\"],[\"roleType\",\"M\"]]}],\"C\":\"Gpf_Rpc_Server\", \"M\":\"run\"}"; string postData = json; byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentType property of the WebRequest. request.ContentType = "application/json; charset=utf-8"; //request.Headers.Add("Accept-Encoding", "gzip,deflate"); // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. Stream dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); // Get the response. //request.Timeout = 740 * 1000; if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog("POST " + request.RequestUri, postData); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Display the status. // Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. var data = new ResponseData(response.StatusCode, reader.ReadToEnd(), response.Headers); // Display the content. // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); if (EDDiscoveryForm.EDDConfig.EDSMLog) { //TODO: Log Status Code too WriteLog(data.Body, ""); } return(data); } catch (WebException ex) { using (WebResponse response = ex.Response) { HttpWebResponse httpResponse = (HttpWebResponse)response; System.Diagnostics.Trace.WriteLine(ex.StackTrace); System.Diagnostics.Trace.WriteLine("WebException : " + ex.Message); System.Diagnostics.Trace.WriteLine($"HTTP Error code: {httpResponse.StatusCode}"); System.Diagnostics.Trace.WriteLine(ex.StackTrace); if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog("WebException" + ex.Message, ""); WriteLog($"HTTP Error code: {httpResponse.StatusCode}", ""); } return(new ResponseData(httpResponse.StatusCode)); } } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine("Exception : " + ex.Message); System.Diagnostics.Trace.WriteLine(ex.StackTrace); if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog("Exception" + ex.Message, ""); } return(new ResponseData(HttpStatusCode.BadRequest)); } }
protected ResponseData RequestPatch(string json, string action, NameValueCollection headers = null) { try { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_serverAddress + action); request.Method = "PATCH"; if (headers != null) { request.Headers.Add(headers); } string postData = json; byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentType property of the WebRequest. request.ContentType = "application/json; charset=utf-8"; //request.Headers.Add("Accept-Encoding", "gzip,deflate"); // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. Stream dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); // Get the response. //request.Timeout = 740 * 1000; if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog("PATCH " + request.RequestUri, postData); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Display the status. // Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. var data = new ResponseData(response.StatusCode, reader.ReadToEnd(), response.Headers); // Display the content. // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog(data.Body, ""); } return(data); } catch (WebException ex) { using (WebResponse response = ex.Response) { HttpWebResponse httpResponse = (HttpWebResponse)response; System.Diagnostics.Trace.WriteLine(ex.StackTrace); System.Diagnostics.Trace.WriteLine("WebException : " + ex.Message); System.Diagnostics.Trace.WriteLine($"HTTP Error code: {httpResponse.StatusCode}"); System.Diagnostics.Trace.WriteLine(ex.StackTrace); if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog("WebException" + ex.Message, ""); WriteLog($"HTTP Error code: {httpResponse.StatusCode}", ""); } return(new ResponseData(httpResponse.StatusCode)); } } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine("Exception : " + ex.Message); System.Diagnostics.Trace.WriteLine(ex.StackTrace); if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog("Exception" + ex.Message, ""); } return(new ResponseData(HttpStatusCode.BadRequest)); } }
protected ResponseData RequestDelete(string action, NameValueCollection headers = null) { try { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_serverAddress + action); request.Method = "DELETE"; // Set the ContentType property of the WebRequest. request.ContentType = "application/json; charset=utf-8"; request.Headers.Add("Accept-Encoding", "gzip,deflate"); if (headers != null) { request.Headers.Add(headers); } request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; if (EDDiscoveryForm.EDDConfig.EDSMLog) WriteLog("DELETE " + request.RequestUri, ""); // Get the response. //request.Timeout = 740 * 1000; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Display the status. // Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. Stream dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. var data = new ResponseData(response.StatusCode, reader.ReadToEnd(), response.Headers); // Display the content. // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog(data.Body, ""); } return data; } catch (WebException ex) { using (WebResponse response = ex.Response) { HttpWebResponse httpResponse = (HttpWebResponse)response; System.Diagnostics.Trace.WriteLine(ex.StackTrace); System.Diagnostics.Trace.WriteLine("WebException : " + ex.Message); System.Diagnostics.Trace.WriteLine($"HTTP Error code: {httpResponse.StatusCode}"); System.Diagnostics.Trace.WriteLine(ex.StackTrace); if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog("WebException" + ex.Message, ""); WriteLog($"HTTP Error code: {httpResponse.StatusCode}", ""); } return new ResponseData(httpResponse.StatusCode); } } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine("Exception : " + ex.Message); System.Diagnostics.Trace.WriteLine(ex.StackTrace); if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog("Exception" + ex.Message, ""); } return new ResponseData(HttpStatusCode.BadRequest); } }
protected ResponseData RequestPost(string json, string action, NameValueCollection headers = null) { try { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_serverAddress + action); // Set the Method property of the request to POST. request.Method = "POST"; if (headers != null) { request.Headers.Add(headers); } // Create POST data and convert it to a byte array. //WRITE JSON DATA TO VARIABLE D //string postData = "{\"requests\":[{\"C\":\"Gpf_Auth_Service\", \"M\":\"authenticate\", \"fields\":[[\"name\",\"value\"],[\"Id\",\"\"],[\"username\",\"[email protected]\"],[\"password\",\"ab9ce908\"],[\"rememberMe\",\"Y\"],[\"language\",\"en-US\"],[\"roleType\",\"M\"]]}],\"C\":\"Gpf_Rpc_Server\", \"M\":\"run\"}"; string postData = json; byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentType property of the WebRequest. request.ContentType = "application/json; charset=utf-8"; //request.Headers.Add("Accept-Encoding", "gzip,deflate"); // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. Stream dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); // Get the response. //request.Timeout = 740 * 1000; if (EDDiscoveryForm.EDDConfig.EDSMLog) WriteLog("POST " + request.RequestUri, postData); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Display the status. // Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. var data = new ResponseData(response.StatusCode, reader.ReadToEnd(), response.Headers); // Display the content. // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); if (EDDiscoveryForm.EDDConfig.EDSMLog) { //TODO: Log Status Code too WriteLog(data.Body, ""); } return data; } catch (WebException ex) { using (WebResponse response = ex.Response) { HttpWebResponse httpResponse = (HttpWebResponse)response; System.Diagnostics.Trace.WriteLine(ex.StackTrace); System.Diagnostics.Trace.WriteLine("WebException : " + ex.Message); System.Diagnostics.Trace.WriteLine($"HTTP Error code: {httpResponse.StatusCode}"); System.Diagnostics.Trace.WriteLine(ex.StackTrace); if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog("WebException" + ex.Message, ""); WriteLog($"HTTP Error code: {httpResponse.StatusCode}", ""); } return new ResponseData(httpResponse.StatusCode); } } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine("Exception : " + ex.Message); System.Diagnostics.Trace.WriteLine(ex.StackTrace); if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog("Exception" + ex.Message, ""); } return new ResponseData(HttpStatusCode.BadRequest); } }
protected ResponseData RequestPatch(string json, string action, NameValueCollection headers = null) { try { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_serverAddress + action); request.Method = "PATCH"; if (headers != null) { request.Headers.Add(headers); } string postData = json; byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentType property of the WebRequest. request.ContentType = "application/json; charset=utf-8"; //request.Headers.Add("Accept-Encoding", "gzip,deflate"); // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. Stream dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); // Get the response. //request.Timeout = 740 * 1000; if (EDDiscoveryForm.EDDConfig.EDSMLog) WriteLog("PATCH " + request.RequestUri, postData); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Display the status. // Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. var data = new ResponseData(response.StatusCode, reader.ReadToEnd(), response.Headers); // Display the content. // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog(data.Body, ""); } return data; } catch (WebException ex) { using (WebResponse response = ex.Response) { HttpWebResponse httpResponse = (HttpWebResponse)response; System.Diagnostics.Trace.WriteLine(ex.StackTrace); System.Diagnostics.Trace.WriteLine("WebException : " + ex.Message); System.Diagnostics.Trace.WriteLine($"HTTP Error code: {httpResponse.StatusCode}"); System.Diagnostics.Trace.WriteLine(ex.StackTrace); if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog("WebException" + ex.Message, ""); WriteLog($"HTTP Error code: {httpResponse.StatusCode}", ""); } return new ResponseData(httpResponse.StatusCode); } } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine("Exception : " + ex.Message); System.Diagnostics.Trace.WriteLine(ex.StackTrace); if (EDDiscoveryForm.EDDConfig.EDSMLog) { WriteLog("Exception" + ex.Message, ""); } return new ResponseData(HttpStatusCode.BadRequest); } }
private ResponseData getResponseData(HttpWebResponse response) { var dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); var data = new ResponseData(response.StatusCode, reader.ReadToEnd(), response.Headers); reader.Close(); dataStream.Close(); return data; }
private void OutputErrors(ResponseData response) { if ((int)response.StatusCode >= 400) { var errorMsg = response.JsonApiErrorMessage(); MessageBox.Show(errorMsg, "Unable to save", MessageBoxButtons.OK, MessageBoxIcon.Error); } }