private void addCompletedTransaction(HttpProgressInfo info) { var ve = new VisualiserEntry(); ve.Url = info.Url.ToString(); ve.Method = info.Method; ve.Ordinal = (requestCount++).ToString(); ve.State = "C"; var sb = new StringBuilder(); sb.AppendFormat("Start: {0:HH:mm:ss.ffff}", info.RequestStarted).AppendLine(); sb.AppendFormat(" End: {0:HH:mm:ss.ffff}", info.ResponseCompleted).AppendLine(); sb.AppendLine(); sb.AppendFormat("{0} {1}", info.Method, info.Url).AppendLine(); foreach (var i in info.RequestHeaders.AllKeys) { foreach (var j in info.RequestHeaders.GetValues(i)) { sb.AppendFormat("{0}:{1}", i, j).AppendLine(); } } sb.AppendLine(); sb.Append(info.RequestBody != null ? info.RequestBody.ToString(Newtonsoft.Json.Formatting.Indented) : "").AppendLine(); sb.Append("---------------------------------------").AppendLine(); sb.AppendFormat("{0} {1}", (int)info.ResponseStatus, info.ResponseStatusDescription).AppendLine(); foreach (var i in info.ResponseHeaders.AllKeys) { foreach (var j in info.ResponseHeaders.GetValues(i)) { sb.AppendFormat("{0}:{1}", i, j).AppendLine(); } } sb.AppendLine(); sb.Append(info.ResponseBody != null ? info.ResponseBody.ToString(Newtonsoft.Json.Formatting.Indented) : "").AppendLine(); ve.Body = sb.ToString(); var lr = new ListViewItem(new string[] { ve.Ordinal, ve.Method, ve.State, ve.Url }); lr.Tag = ve; lvSummary.Items.Add(lr); }
private void Client_RequestProgress(object sender, HttpProgressInfo info) { if(info.State == HttpTransactionState.Complete) { BeginInvoke(new handleProgressInfo(addCompletedTransaction), info); } }
public async Task<HttpProgressInfo> ExecuteRequestAsync(string url, Dictionary<string,string> headers = null, string method = "GET", JToken body = null) { var pi = new HttpProgressInfo(); pi.Url = new Uri(url); pi.Method = method; pi.RequestHeaders = new WebHeaderCollection(); var req = WebRequest.CreateHttp(url); req.Method = method; if (headers != null) { foreach (var i in headers) { req.Headers.Add(i.Key, i.Value); pi.RequestHeaders.Add(i.Key, i.Value); } } pi.RequestBody = body == null ? null : body.DeepClone(); pi.RequestStarted = DateTime.UtcNow; pi.State = HttpTransactionState.Started; OnProgress(pi); if (body != null) { using (var rs = await req.GetRequestStreamAsync()) using (var sw = new System.IO.StreamWriter(rs)) { await sw.WriteAsync(body.ToString(Formatting.None)); } } pi.RequestCompleted = DateTime.UtcNow; pi.State = HttpTransactionState.RequestSent; OnProgress(pi); var res = (HttpWebResponse)await req.GetResponseAsync(); pi.State = HttpTransactionState.HeadersReceived; pi.ResponseStarted = DateTime.UtcNow; pi.ResponseStatus = res.StatusCode; pi.ResponseStatusDescription = res.StatusDescription; pi.ResponseHeaders = res.Headers; OnProgress(pi); using (var rs = res.GetResponseStream()) using (var sr = new System.IO.StreamReader(rs, Encoding.UTF8)) { var dataText = await sr.ReadToEndAsync(); pi.State = HttpTransactionState.Complete; pi.ResponseCompleted = DateTime.UtcNow; if (!string.IsNullOrWhiteSpace(dataText)) { var tok = JToken.Parse(dataText); pi.ResponseBody = tok; } } OnProgress(pi); return pi; }
private void OnProgress(HttpProgressInfo pi) { var handler = RequestProgress; if(handler != null) { handler(this, pi); } }