public static HttpResponseMessage PostAsJsonAsync( this HttpClient client, string requestUri, JObject json, Action <string> Logger) { AddUserAgent(client); LogRequest( HttpMethod.Post.Method, client.BaseAddress + requestUri, client.DefaultRequestHeaders, JsonConvert.SerializeObject(json, Formatting.Indented), Logger); HttpResponseMessage response = client.PostAsJsonAsync(requestUri, json).Result; string content = response.EnsureSuccessStatusCode().Content.ReadAsStringAsync().Result; LogResponse( response.StatusCode.ToString(), response.Headers, General.TryFormatJson(content), Logger); return(response); }
public static T GetJson <T>(this HttpClient client, string requestUri, Action <string> Logger) { AddUserAgent(client); LogRequest( HttpMethod.Get.Method, client.BaseAddress + requestUri, client.DefaultRequestHeaders, string.Empty, Logger); HttpResponseMessage response = client.GetAsync(requestUri).Result; string content = response.EnsureSuccessStatusCode().Content.ReadAsStringAsync().Result; LogResponse(response.StatusCode.ToString(), response.Headers, General.TryFormatJson(content), Logger); return(JsonConvert.DeserializeObject <T>(content)); }
private static T CallRestApiWithJsonPayload <T>( this HttpClient client, string requestUri, T json, Action <string> logger, string method) { AddUserAgent(client); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); LogRequest( method, client.BaseAddress + requestUri, client.DefaultRequestHeaders, JsonConvert.SerializeObject(json, Formatting.Indented), logger); HttpResponseMessage response; switch (method.ToUpper()) { case WebRequestMethods.Http.Post: response = client.PostAsJsonAsync(requestUri, json).Result; break; case WebRequestMethods.Http.Put: response = client.PutAsJsonAsync(requestUri, json).Result; break; default: throw new InvalidOperationException(); } string content = response.EnsureSuccessStatusCode().Content.ReadAsStringAsync().Result; LogResponse( response.StatusCode.ToString(), response.Headers, General.TryFormatJson(content), logger); return(JsonConvert.DeserializeObject <T>(content)); }