Get:
HttpClient httpClientGet = new HttpClient();
Task<string> httpClientGetResult = httpClientGet.GetStringAsync(@"https://localhost:7037/api/Values");
Console.WriteLine(httpClientGetResult.GetAwaiter().GetResult());
Post:
HttpClient httpClientPost = new HttpClient();
Task<HttpResponseMessage> httpResponseMessage = httpClientPost.PostAsync(@"https://localhost:7037/api/Values"
, new StringContent(@"""string Test"""
, Encoding.UTF8
, "text/json"));
Task<string> httpClientPostResult = httpResponseMessage.Result.Content.ReadAsStringAsync();
Console.WriteLine(httpClientPostResult.Result);
Example download from
here. Example contains also dummy Web Api for test purposes.
UPDATE: Just a short notice, correct way to use HttpClient synchronously:
var task = Task.Run(() => myHttpClient.GetAsync(someUrl));
task.Wait();
var response = task.Result;
From
here. Also, HttpClient is IDisposable