Answers for "c# httpclient postasync stringcontent"

C#
3

c# httpclient postasync stringcontent

private static async Task PostBasicAsync(object content, CancellationToken cancellationToken)
{
    using (var client = new HttpClient())
    using (var request = new HttpRequestMessage(HttpMethod.Post, Url))
    {
        var json = JsonConvert.SerializeObject(content);
        using (var stringContent = new StringContent(json, Encoding.UTF8, "application/json"))
        {
            request.Content = stringContent;

            using (var response = await client
                .SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
                .ConfigureAwait(false))
            {
                response.EnsureSuccessStatusCode();
            }
        }
    }
}
Posted by: Guest on March-04-2020

Code answers related to "c# httpclient postasync stringcontent"

C# Answers by Framework

Browse Popular Code Answers by Language