public void AddRequestResponse(Request request, Response response) { AddRowInMultiThread(call); void call() { //var strResponseDate = response.getHeaders()?.getHeader("Date")?.ToString().Replace("Date: ", ""); //var converted = DateTime.TryParse(strResponseDate, out var responseDate); count++; var row = new LogRow { Number = count, Type = "LISTENER", TypeDesc = "WIREMOCK LISTENER", Headers = HttpUtils.GetHeaders(request.getHeaders()), Raw = Helper.ResolveBreakLineInCompatibility(request.getHeaders().ToString()), Method = request.getMethod().ToString(), Url = request.getUrl(), UrlAbsolute = request.getAbsoluteUrl(), RequestDate = null, ResponseDate = DateTime.Now, Status = response.getStatus(), StatusMessage = response.getStatusMessage(), Request = request, RequestLog = LoggedRequest.createFrom(request), ResponseLog = LoggedResponse.from(response), Response = response }; AddNewRow(row); }; }
private async void ShowResponse(DateTime t1, DateTime t2, HttpWebRequest request, HttpWebResponse response) { using (var s = response.GetResponseStream()) { using (var sr = new StreamReader(s, Encoding.UTF8)) { var content = await sr.ReadToEndAsync(); txtResponseBody.TextValue = Helper.ResolveBreakLineInCompatibility(content); } } var headers = HttpUtils.GetHeaders(response); stsTimeValue.Text = (t2 - t1).ToString(); stsStatusValue.Text = $"{(int)response.StatusCode} ({response.StatusDescription})"; txtRequestHeadersFinal.TextValue = HttpUtils.GetHeadersAsString(headers); txtResponseHeaders.TextValue = $"{(int)response.StatusCode} {response.StatusDescription}\r\n"; txtResponseHeaders.TextValue += HttpUtils.GetHeadersAsString(HttpUtils.GetHeaders(response)); btnExecute.Enabled = true; var contentType = HttpUtils.GetHeaderValue(headers, "content-type"); if (contentType != null) { contentType = contentType.ToLower(); if (contentType.Contains("xml")) { txtResponseBody.Language = AdvancedEditor.LanguageSupported.XML; } else if (contentType.Contains("html")) { txtResponseBody.Language = AdvancedEditor.LanguageSupported.HTML; } else if (contentType.Contains("json")) { txtResponseBody.Language = AdvancedEditor.LanguageSupported.Json; } else if (contentType.Contains("javascript")) { txtResponseBody.Language = AdvancedEditor.LanguageSupported.JS; } } }
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; } }