public static async Task <httpClientResponse <String> > GetStringAsync(string Url) { httpClientResponse <String> result = new httpClientResponse <String>(); if (!String.IsNullOrWhiteSpace(Url) && Uri.IsWellFormedUriString(Url, UriKind.Absolute)) { Stopwatch s = new Stopwatch(); s.Start(); var response = await _client.GetAsync(Url); s.Stop(); result.execTime = String.Format("_1_{0}ms", s.ElapsedMilliseconds); if (response.IsSuccessStatusCode) { s.Restart(); var postResultData = await response.Content.ReadAsStringAsync(); s.Stop(); result.execTime += String.Format("_2_{0}ms", s.ElapsedMilliseconds); s.Restart(); result.result = postResultData; } s.Stop(); result.execTime += String.Format("_3_{0}ms", s.ElapsedMilliseconds); result.StatusCode = response.StatusCode; } return(result); }
public static async Task <httpClientResponse <T> > PostFormAsync <T>(string Url, FormUrlEncodedContent content, NameValueCollection header = null) where T : class, new() { httpClientResponse <T> result = new httpClientResponse <T>(); if (!String.IsNullOrWhiteSpace(Url) && Uri.IsWellFormedUriString(Url, UriKind.Absolute) && content != null) { if (header != null) { foreach (var key in header.AllKeys) { content.Headers.Add(key, header.Get(key)); } } Stopwatch s = new Stopwatch(); s.Start(); var response = await _client.PostAsync(Url, content); s.Stop(); result.execTime = String.Format("_1_{0}ms", s.ElapsedMilliseconds); if (response.IsSuccessStatusCode) { s.Restart(); var postResultData = await response.Content.ReadAsStringAsync(); s.Stop(); result.execTime += String.Format("_2_{0}ms", s.ElapsedMilliseconds); s.Restart(); result.result = JsonConvert.DeserializeObject <T>(postResultData); } s.Stop(); result.execTime += String.Format("_3_{0}ms", s.ElapsedMilliseconds); result.StatusCode = response.StatusCode; } return(result); }
public static async Task <httpClientResponse <T> > PostAsJsonAsync <T>(string Url, string postData) { httpClientResponse <T> result = new httpClientResponse <T>(); if (!String.IsNullOrWhiteSpace(Url) && Uri.IsWellFormedUriString(Url, UriKind.Absolute)) { HttpContent contentPost = new StringContent(postData, System.Text.Encoding.UTF8, "application/json"); Stopwatch s = new Stopwatch(); s.Start(); var response = await _client.PostAsync(Url, contentPost); s.Stop(); result.execTime = String.Format("_1_{0}ms", s.ElapsedMilliseconds); if (response.IsSuccessStatusCode) { s.Restart(); var postResultData = await response.Content.ReadAsStringAsync(); s.Stop(); result.execTime += String.Format("_2_{0}ms", s.ElapsedMilliseconds); s.Restart(); result.result = JsonConvert.DeserializeObject <T>(postResultData); } s.Stop(); result.execTime += String.Format("_3_{0}ms", s.ElapsedMilliseconds); result.StatusCode = response.StatusCode; } return(result); }