using System.Net.Http; public class Example { public async Task RunAsync() { using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync("https://example.com"); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } } }
using System.Net.Http; public class Example { public async Task RunAsync() { using (HttpClient client = new HttpClient()) { var content = new StringContent("{\"name\":\"John\"}", Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync("https://example.com", content); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } } }In this example, we create a new instance of the HttpClient class, and use the PostAsync method to make a POST request to the specified URL. We also include a JSON string as the request body. We then read the response body as a string and write it to the console. The package library for the IHttpClient interface is the System.Net.Http package. This package is included in the .NET framework, so it does not need to be installed separately.