private static async Task <HttpResponseMessage> PostJsonRequest(string url, string jsonContent) { try { using (var client = new HttpClient()) { var content = new StringContent(jsonContent.ToString(), Encoding.UTF8, "application/json"); // Lambda expression executed // ReSharper disable AccessToDisposedClosure var result = await TransientRetry.Do(() => client.PostAsync(url, content), new TimeSpan(0, 0, 0, 3)); // ReSharper restore AccessToDisposedClosure return(result); } } catch (Exception ex) { throw new Exception(ex.Message); } }
public async Task <HttpResponseMessage> PostRequestBody(string url, string body, string accessToken, bool prependBasePath = true) { using (var client = new HttpClient()) { // ReSharper disable once AccessToDisposedClosure var result = await TransientRetry.Do(async() => { var finalUrl = prependBasePath ? basePath + url : url; var content = new StringContent(body, Encoding.UTF8, "application/json"); var message = new HttpRequestMessage(HttpMethod.Post, finalUrl) { Content = content }; message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); try { return(client.SendAsync(message)); } catch (WebException e) { if ((e.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.Unauthorized) { // The token is expired, get a new one and retry Settings.GenericToken = (await GetTokenForDataStore()).AccessToken; // ReSharper disable once AccessToDisposedClosure return(client.SendAsync(message)); } else { throw e; } } }, new TimeSpan(0, 0, 0, 3)); return(await result); } }