Provides a framework for interacting with HTTP services. It supports both HTTP/1.1 and HTTP/2 protocols. This class is a key component of .NET's support for web services and RESTful APIs.
The System.Net.Http
class represents a client for making HTTP requests. It allows you to send requests to web servers and receive responses, which can then be processed by your application.
GetAsync(string url, CancellationToken cancellationToken = default)
: Sends an HTTP GET request to the specified URL.PostAsync(string url, HttpContent content, CancellationToken cancellationToken = default)
: Sends an HTTP POST request to the specified URL with the provided content.PutAsync(string url, HttpContent content, CancellationToken cancellationToken = default)
: Sends an HTTP PUT request.DeleteAsync(string url, CancellationToken cancellationToken = default)
: Sends an HTTP DELETE request.
using System.Net.Http;
using System.Threading.Tasks;
public async Task GetWebContentAsync(string url)
{
using (var httpClient = new HttpClient())
{
var response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}