private async void WebRequest() { btnExecute.Enabled = false; CleanResponses(); HttpWebRequest webRequest = null; var start = DateTime.Now; try { var headers = HttpUtils.GetHeaders(txtRequestHeaders.TextValue, false, false); webRequest = (HttpWebRequest)System.Net.WebRequest.Create(txtUrl.TextValue); webRequest.AllowAutoRedirect = this.chkAutoRedirect.Checked; webRequest.KeepAlive = this.chkKeepAlive.Checked; headers.Remove("method"); headers.Remove("url"); headers.Remove("protocol"); headers.Remove("protocol-version"); if (webRequest != null) { webRequest.Method = cmbVerb.Text; webRequest.Timeout = (int)txtTimeout.Value; if (headers.Any(f => f.Key.ToLower() == "accept-encoding")) { webRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; } var contentTypeHeader = headers.FirstOrDefault(f => f.Key.ToLower() == "content-type"); if (contentTypeHeader.Key != null) { webRequest.ContentType = contentTypeHeader.Value; } webRequest.ServicePoint.Expect100Continue = chk100Expect.Checked; // Obrigatory before body foreach (var h in headers) { try { var nameLower = h.Key.ToLower(); var valueLower = h.Value.ToLower(); switch (nameLower) { case "host": break; case "content-length": if (!this.chkAutoContentLength.Checked) { webRequest.ContentLength = Convert.ToInt64(h.Value); } break; case "accept-encoding": break; case "proxy-connection": break; case "content-type": webRequest.ContentType = h.Value; break; case "expect": if (h.Value?.ToLower() == "100-expect") { webRequest.ServicePoint.Expect100Continue = true; } break; case "user-agent": webRequest.UserAgent = h.Value; break; case "accept": webRequest.Accept = h.Value; break; case "referer": webRequest.Referer = h.Value; break; case "connection": if (valueLower == "keep-alive") { webRequest.KeepAlive = true; } break; default: webRequest.Headers.Add(h.Key, h.Value); break; } } catch (Exception ex) { Helper.MessageBoxError(ex.Message); } } // Obrigatory after headers if (!string.IsNullOrWhiteSpace(this.txtRequestBody.TextValue)) { var data = Encoding.ASCII.GetBytes(this.txtRequestBody.TextValue); if (this.chkAutoContentLength.Checked) { webRequest.ContentLength = data.Length; } var newStream = webRequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); } start = DateTime.Now; txtRequestHeadersFinal.TextValue = HttpUtils.GetHeadersAsString(HttpUtils.GetHeaders(webRequest)); var response = await webRequest.GetResponseAsync(); ShowResponse(start, DateTime.Now, webRequest, (HttpWebResponse)response); } } catch (WebException ex) { if (ex.Response != null) { ShowResponse(start, DateTime.Now, webRequest, (HttpWebResponse)ex.Response); } else { Helper.MessageBoxError(ex.Message); } btnExecute.Enabled = true; } catch (Exception ex) { Helper.MessageBoxError(ex.Message); btnExecute.Enabled = true; } }
private ContextMenuStrip GetMenuTripRow(LogRow row) { // context menu var menu = new ContextMenuStrip(); var viewRequestMenu = new ToolStripMenuItem(); var viewResponseMenu = new ToolStripMenuItem(); var viewRawMenu = new ToolStripMenuItem(); var viewInComposerMenu = new ToolStripMenuItem(); var compareMenu = new ToolStripMenuItem(); menu.Items.AddRange(new ToolStripMenuItem[] { viewRequestMenu, viewResponseMenu, viewRawMenu, viewInComposerMenu, compareMenu }); menu.Opening += (a, b) => { if (row.Request == null) { viewRawMenu.Visible = true; viewRequestMenu.Visible = false; viewResponseMenu.Visible = false; viewInComposerMenu.Visible = false; compareMenu.Visible = false; } else { viewRawMenu.Visible = false; viewRequestMenu.Visible = true; viewResponseMenu.Visible = true; viewInComposerMenu.Visible = true; compareMenu.Visible = true; } }; // view request viewRequestMenu.Text = Resource.viewRequestMenu; viewRequestMenu.Click += (a, b) => { var frmStart = new FormTextView(master, row.UrlAbsolute, row.RequestLog.ToString()); master.TabMaster.AddTab(frmStart, row, row.Url + " (Request)"); }; // view response viewResponseMenu.Text = Resource.viewResponseMenu; viewResponseMenu.Click += (a, b) => { var frmStart = new FormTextView(master, row.UrlAbsolute, row.Response.ToString()); master.TabMaster.AddTab(frmStart, row, row.Url + " (Response)"); }; // view raw viewRawMenu.Text = Resource.viewRawMenu; viewRawMenu.Click += (a, b) => { var frmStart = new FormTextView(master, row.UrlAbsolute, row.Raw); master.TabMaster.AddTab(frmStart, row, row.Url + " (Raw)"); }; // view in composer viewInComposerMenu.Text = Resource.viewInComposerMenu; viewInComposerMenu.Click += (a, b) => { var requestHeaders = GetHeaders(row.RequestLog); var requestBody = row.RequestLog.getBodyAsString(); var responseHeaders = GetHeaders(row.Response); var responseBody = row.Response.getBodyAsString(); var frmComposer = new FormWebRequest(row.Method, row.UrlAbsolute, requestHeaders, requestBody, responseHeaders, responseBody); master.TabMaster.AddTab(frmComposer, row, row.Url); }; // view in composer compareMenu.Text = Resource.compareMenu; compareMenu.Click += (a, b) => { var strBuilder = new StringBuilder(); StringBuilder strBuilderFormatted = null; var requestHeaders = GetHeaders(row.RequestLog); var requestBody = row.RequestLog.getBodyAsString(); strBuilder.AppendLine($"{row.Method} {row.UrlAbsolute}"); strBuilder.AppendLine(); if (requestBody?.Length == 0) { strBuilder.Append(GetHeadersAsString(requestHeaders)); } else { var strHeaders = GetHeadersAsString(requestHeaders); strBuilder.AppendLine(strHeaders); strBuilder.AppendLine(); strBuilder.Append(requestBody); // formatted body var contentType = HttpUtils.GetHeaderValue(row.Headers, "content-type"); string requestBodyFormatted = null; if (contentType?.ToLower().Contains("json") == true) { requestBodyFormatted = Helper.FormatToJson(requestBody, false); } else if (contentType?.ToLower().Contains("xml") == true) { requestBodyFormatted = Helper.FormatToXml(requestBody, false); } if (requestBodyFormatted != null) { strBuilderFormatted = new StringBuilder(); strBuilderFormatted.AppendLine(strHeaders); strBuilderFormatted.AppendLine(); strBuilderFormatted.Append(requestBodyFormatted); } } var frmCompare = new FormCompare(this.master, strBuilderFormatted?.ToString() ?? strBuilder.ToString()); master.TabMaster.AddTab(frmCompare, row, row.Url); }; return(menu); }