private void AddRunHistory(WebRequest Request, string RequestHeader, string RequestBody, HttpWebResponse Response, string JsonResponse) { int maxHistoryCount = 30; // Keep only 30 history. while (runHistory.RunInfo.Count >= maxHistoryCount) { runHistory.RunInfo.RemoveAt(0); } string statusCode; string headers; if (Response == null) { statusCode = "Error"; headers = ""; } else { statusCode = Response.StatusCode.ToString(); headers = Response.Headers.ToString(); } RunInformation newRunInfo = new RunInformation { ExecutionID = DateTime.UtcNow.ToString("yyyyMMddHHmmssfff"), RequestUrl = Request.RequestUri.ToString(), RequestMethod = Request.Method, RequestHeader = RequestHeader, RequestCompleteHeader = Request.Headers.ToString(), RequestBody = RequestBody, ResponseStatusCode = statusCode, ResponseHeader = headers, ResponseBody = JsonResponse }; // Add new history. runHistory.RunInfo.Add(newRunInfo); // Update the listbox. listBox_RunHistory.Items.Clear(); for (int i = runHistory.RunInfo.Count - 1; i >= 0; i--) { listBox_RunHistory.Items.Add(runHistory.RunInfo[i]); } // Save the file. try { FileStream stream = new FileStream(Path.Combine(Application.StartupPath, "RunHistory.xml"), FileMode.Create); using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8)) { // Serialize System.Xml.Serialization.XmlSerializerNamespaces nameSpace = new System.Xml.Serialization.XmlSerializerNamespaces(); nameSpace.Add(string.Empty, string.Empty); System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(RunHistory)); serializer.Serialize(writer, runHistory, nameSpace); writer.Flush(); } } catch (Exception ex) { MessageBox.Show("The run history file could not be saved." + Environment.NewLine + Environment.NewLine + ex.Message, "Office365APIEditor"); } }
private void ShowRunHistoryInMainPanel(RunInformation runInfo) { // Show the details of selected run history. textBox_Request.Text = runInfo.RequestUrl; dataGridView_RequestHeader.Rows.Clear(); string[] headers = runInfo.RequestHeader.Replace("\r", "").Split('\n'); foreach (string header in headers) { if (header == "") { continue; } string[] delimiter = { ": " }; string[] headerParts = header.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); string headerName = ""; string headerValue = ""; if (headerParts.Length == 0) { // invalid format continue; } else if (headerParts.Length == 1) { // header value is missing. headerName = headerParts[0]; } else if (headerParts.Length == 2) { headerName = headerParts[0]; headerValue = headerParts[1]; } else { // Header value contains ": " . headerName = headerParts[0]; headerValue = header.Remove(0, headerName.Length + 2); } dataGridView_RequestHeader.Rows.Add(headerName, headerValue); } textBox_RequestBody.Text = runInfo.RequestBody; switch (runInfo.RequestMethod.ToUpper()) { case "GET": radioButton_GET.Select(); break; case "POST": radioButton_POST.Select(); break; case "PATCH": radioButton_PATCH.Select(); break; case "DELETE": radioButton_DELETE.Select(); break; default: break; } label_StatusCode.Text = runInfo.ResponseStatusCode; textBox_ResponseHeaders.Text = runInfo.ResponseHeader.Replace("\n", Environment.NewLine); originalJsonResponse = runInfo.ResponseBody; indentedJsonResponse = ""; decodedJsonResponse = ""; indentedAndDecodedJsonResponse = ""; textBox_ResponseBody.Text = ShapeJsonResponseIfNeeded(originalJsonResponse); }
private void ShowRunHistoryInMainPanel(RunInformation runInfo) { // Show the details of selected run history. textBox_Request.Text = runInfo.RequestUrl; dataGridView_RequestHeader.Rows.Clear(); string[] headers = runInfo.RequestHeader.Replace("\r", "").Split('\n'); foreach (string header in headers) { if (header == "") { continue; } string[] delimiter = { ": " }; string[] headerParts = header.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); string headerName = ""; string headerValue = ""; if (headerParts.Length == 0) { // invalid format continue; } else if (headerParts.Length == 1) { // header value is missing. headerName = headerParts[0]; } else if (headerParts.Length == 2) { headerName = headerParts[0]; headerValue = headerParts[1]; } else { // Header value contains ": " . headerName = headerParts[0]; headerValue = header.Remove(0, headerName.Length + 2); } dataGridView_RequestHeader.Rows.Add(headerName, headerValue); } textBox_RequestBody.Text = runInfo.RequestBody; switch (runInfo.RequestMethod.ToUpper()) { case "GET": radioButton_GET.Select(); break; case "POST": radioButton_POST.Select(); break; case "PATCH": radioButton_PATCH.Select(); break; case "DELETE": radioButton_DELETE.Select(); break; default: break; } label_StatusCode.Text = runInfo.ResponseStatusCode; textBox_ResponseHeaders.Text = runInfo.ResponseHeader.Replace("\n", Environment.NewLine); bool isImage = false; bool isCsv = false; if (textBox_ResponseHeaders.Text.Contains("Content-Type: image/jpeg")) { isImage = true; } isCsv = Regex.Match(textBox_ResponseHeaders.Text, "attachment; filename=\".*\\.csv\"", RegexOptions.None).Success; originalJsonResponse = runInfo.ResponseBody; indentedJsonResponse = ""; decodedJsonResponse = ""; indentedAndDecodedJsonResponse = ""; textBox_ResponseBody.Text = ShapeJsonResponseIfNeeded(originalJsonResponse); if (isImage) { // Create new bitmap object to display the response byte[] binaryResponse = Convert.FromBase64String(originalJsonResponse); MemoryStream memoryStream = new MemoryStream(); byte[] pictureData = binaryResponse; memoryStream.Write(pictureData, 0, Convert.ToInt32(pictureData.Length)); Bitmap bitmapResponse = new Bitmap(memoryStream, false); memoryStream.Dispose(); pictureBox_Photo.Image = bitmapResponse; // Show Preview tab tabControl_Response.SelectTab(2); } else if (isCsv) { // Create DataTable StringReader stringReader = new StringReader(originalJsonResponse); string line = stringReader.ReadLine(); DataTable dataTable = new DataTable(); DataRow dataRow; if (line != "") { // Create headers. foreach (var column in line.Split(',')) { dataTable.Columns.Add(column); } // Add rows. while ((line = stringReader.ReadLine()) != null) { dataRow = dataTable.NewRow(); dataRow.ItemArray = line.Split(','); dataTable.Rows.Add(dataRow); } } dataGridView_CSV.DataSource = dataTable; // Show CSV tab tabControl_Response.SelectTab(3); } else { // Show Body tab tabControl_Response.SelectTab(1); textBox_ResponseBody.Select(0, 0); } // Show the picturebox in preview tab if response is image. pictureBox_Photo.Visible = isImage; // Show the DataGridView and Link in CSV tab if response is CSV. dataGridView_CSV.Visible = isCsv; linkLabel_SaveCsvResponse.Visible = isCsv; }