/// <summary> /// Perform HTTP operation to SECURE API /// </summary> /// <param name="mediaType">MediaType, enum describing the possible mediatypes</param> /// <param name="httpOP">HTTP, enum describing the possible HTTP operations</param> /// <param name="tsItem">ToolStripMenuItem, the menu item that was clicked</param> private async void PerformHTTP_Async(string mediaType, HTTP httpOP, ToolStripMenuItem tsItem) { try { // clear text boxes this.txtSubmitHTTP.Text = string.Empty; this.txtReceivedHTTP.Text = string.Empty; HttpContent content = null; string requestString = string.Empty; // get String array of API string[] apiName = this.GetAPINameFromToolStripMenuItem(tsItem); // get Parameters for selected API string[] parameters = this.GetParameterListFromToolStripMenuItem(tsItem); ArrayList nonUserParameters = new ArrayList(); ArrayList userParameters = new ArrayList(); ArrayList contentParameters = new ArrayList(); // separate parameters into parameters that require user input // and parameters that can be set from known data for (int i = 0; i < parameters.Length; i++) { if (TestController.TextInputParameters.Contains(parameters[i])) { userParameters.Add(parameters[i]); } else if (TestController.ContentParameters.Contains(parameters[i])) { contentParameters.Add(parameters[i]); } else { throw new Exception("Unknown API parameter"); } } DictionaryEntry[] apiParameters = new DictionaryEntry[userParameters.Count + nonUserParameters.Count]; if (userParameters.Count > 0 || contentParameters.Count > 0) { // Display form for input of parameters string[] allInputParameters = null; if (userParameters.Count > 0 && contentParameters.Count > 0) { allInputParameters = new string[userParameters.Count + contentParameters.Count]; ((string[])userParameters.ToArray(typeof(string))).CopyTo(allInputParameters, 0); ((string[])contentParameters.ToArray(typeof(string))).CopyTo(allInputParameters, userParameters.Count); } else if (userParameters.Count == 0 && contentParameters.Count != 0) { allInputParameters = ((string[])contentParameters.ToArray(typeof(string))); } else if (userParameters.Count != 0 && contentParameters.Count == 0) { allInputParameters = ((string[])userParameters.ToArray(typeof(string))); } InputForm inputForm = new InputForm(allInputParameters); DialogResult DR = inputForm.ShowDialog(this); if (DR == System.Windows.Forms.DialogResult.OK) { DictionaryEntry[] userInputParameters = inputForm.Parameters; ArrayList contentInputParameters = new ArrayList(); for (int i = 0; i < parameters.Length; i++) { if (userParameters.Contains(parameters[i])) { foreach (DictionaryEntry dEntry in userInputParameters) { if (dEntry.Key.ToString() == parameters[i]) { apiParameters[i] = dEntry; } } } else if (contentParameters.Contains(parameters[i])) { foreach (DictionaryEntry dEntry in userInputParameters) { if (dEntry.Key.ToString() == parameters[i]) { contentInputParameters.Add(dEntry); } } } } if (contentInputParameters.Count == 1) { requestString = ((DictionaryEntry)contentInputParameters[0]).Value.ToString(); content = new StringContent( requestString, Encoding.UTF8, mediaType); } else if (contentInputParameters.Count > 1) { // This should not happen! No current API's take more than one XML parameter throw new Exception("Multiple XML Parameters!"); } } else { // User hit cancel in InputForm return; } } //======================================================================================================================================= // display loadingbar form this.LoadingBarShow("Connecting to SECURE"); // perform HTTP OP string responseText = await this._testController.SECURE_API_Async(mediaType, httpOP, apiName, apiParameters, content); // display HTTP Response this.txtReceivedHTTP.Text = TestController.FormatXML(responseText); // special operations switch (tsItem.Name) { case "logoutToolStripMenuItem": { this._testController.Session = null; //this._testController.SessionTokenID = string.Empty; this.SessionTokenLabel.Text = "NOT LOGGED IN"; this.EnableMenus(false); //this.tabMain.SelectedTab = tpLogin; //this.menuStripMain.Enabled = false; break; } } // refresh StatusStrip this.statusStrip1.Refresh(); this.urlLabel.Text = string.Format("HTTP {0} | URL: {1}", this._testController.Last_HTTPstatus, this._testController.Last_URL); // display sent HTTP request string formattedRequestString = TestController.FormatXML(requestString); this.txtSubmitHTTP.Text = string.Format(content == null ? "{0}{1}" : "{0}\r\n\nContent:\r\n{1}", this._testController.Last_HTTPResponse.RequestMessage.ToString(), formattedRequestString); this.statusStrip2.Refresh(); } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // Stop displaying loading bar form this.LoadingBarHide(); } }
/// <summary> /// Log in to SECURE /// </summary> private async void PerformLoginAsync() { try { this.LoadingBarShow("Logging In"); // clear text boxes this.txtSubmitHTTP.Text = string.Empty; this.txtReceivedHTTP.Text = string.Empty; // Show input box InputForm inputForm = new InputForm( new DictionaryEntry[] { new DictionaryEntry("UserName", this.defaultUserName), new DictionaryEntry("Password", this.defaultPassword) }); DialogResult DR = inputForm.ShowDialog(this); if (DR != System.Windows.Forms.DialogResult.OK) { return; } DictionaryEntry[] userInputParameters = inputForm.Parameters; string userName = string.Empty; string password = string.Empty; foreach (DictionaryEntry paramEntry in userInputParameters) { if (paramEntry.Key.ToString() == "UserName") { userName = paramEntry.Value.ToString(); } if (paramEntry.Key.ToString() == "Password") { password = paramEntry.Value.ToString(); } } string responseText = await this._testController.LoginAsync( userName, password, null, true); this.menuStripMain.Enabled = true; string httpText = TestController.FormatXML(responseText); this.tabMain.SelectedTab = this.tpHTTP; this.txtReceivedHTTP.Text = TestController.FormatXML(responseText); this.txtSubmitHTTP.Text = this._testController.Last_HTTPResponse.RequestMessage.ToString(); this.SessionTokenLabel.Text = (this._testController.SessionTokenID != string.Empty) ? ("SessionTokenID: " + this._testController.SessionTokenID) : this.SessionTokenLabel.Text; this.EnableMenus(this._testController.SessionTokenID != string.Empty); this.statusStrip1.Refresh(); this.urlLabel.Text = string.Format("HTTP {0}{1} | URL: {2}", this._testController.Last_HTTPstatus, ((this._testController.Last_HTTPstatus == 500 || this._testController.Last_HTTPstatus == 404) ? ": Login Failed" : (this._testController.Last_HTTPstatus == 201 ? ": Need to change password" : string.Empty)), this._testController.Last_URL); this.statusStrip2.Refresh(); } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { this.LoadingBarHide(); } }