GET, POST, PUT, DELETE Requests | C# HttpClient

C# HttpClient is a class in the .NET framework used for sending HTTP requests and receiving HTTP responses from a resource identified by a URI. It provides a convenient way to make HTTP requests from .NET applications and can be used to perform various operations such as sending GET, POST, PUT, DELETE requests, etc. It's designed to be used for modern, RESTful APIs and supports asynchronous programming.

HTTP request methods

HTTP (Hypertext Transfer Protocol) defines a set of request methods to indicate the desired action to be performed for a given resource. The following are the most common HTTP request methods:

Request Description
GET Retrieves data from a specified resource.
POST Submits data to be processed to a specified resource.
PUT Updates a current resource with new data.
DELETE Deletes a specified resource.
HEAD Retrieves only the header information from a specified resource.
CONNECT Establishes a network connection to a specified resource
OPTIONS Describes the communication options for the target resource.
PATCH Applies partial modifications to a specified resource.

These methods are used to indicate the type of operation to be performed on the specified resource and are specified in the request header sent to the server.

C# HttpClient GET request

The GetAsync method is used to send the GET request. The method returns a Task of type HttpResponseMessage, which represents the response from the server.

Follwoing example shows how you can use C# HttpClient to send a GET request:

using System; using System.Net.Http; using System.Threading.Tasks; namespace HttpClientExample { class Program { static async Task Main(string[] args) { using (var client = new HttpClient()) { try { var response = await client.GetAsync("https://www.example.com/api/data"); //URL to send the request to. response.EnsureSuccessStatusCode(); // Check the response was successful. var content = await response.Content.ReadAsStringAsync();// Read response as a string. Console.WriteLine(content); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } } } }

Explanation:

In the above example, the GetAsync method is used to send the GET request. The method returns a Task of type HttpResponseMessage, which represents the response from the server. The EnsureSuccessStatusCode method is used to check if the response was successful (returned with a status code in the 200-299 range), and if not, it throws an exception. The response content can be read as a string using the ReadAsStringAsync method.

C# HttpClient POST request

The PostAsJsonAsync method is used to send the POST request. The method takes two arguments: the URL to send the request to, and the request content.

The following example shows how you can use C# HttpClient to send a POST request:

using System; using System.Net.Http; using System.Net.Http.Json; using System.Threading.Tasks; namespace HttpClientExample { class Program { static async Task Main(string[] args) { using (var client = new HttpClient()) { try { //URL to send the request to. var request = new { name = "Warner Hope", age = 30 }; var response = await client.PostAsJsonAsync("https://www.example.com/api/data", request); // Send the POST request response.EnsureSuccessStatusCode();// Check the responses var content = await response.Content.ReadAsStringAsync(); // Read the responses Console.WriteLine(content); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } } } }

Explanation:

In the above example, the PostAsJsonAsync method is used to send the POST request. The method takes two arguments: the URL to send the request to, and the request content. The request content can be any object, which is then serialized to JSON and sent in the request body. The method returns a Task of type HttpResponseMessage, which represents the response from the server. The EnsureSuccessStatusCode method is used to check if the response was successful (returned with a status code in the 200-299 range), and if not, it throws an exception. The response content can be read as a string using the ReadAsStringAsync method.

C# HttpClient PUT Request


how to c# httpclient

The PutAsJsonAsync method is used to send the PUT request. The method takes two arguments: the URL to send the request to, and the request content.

The following example shows how you can use C# HttpClient to send a PUT request:

using System; using System.Net.Http; using System.Net.Http.Json; using System.Threading.Tasks; namespace HttpClientExample { class Program { static async Task Main(string[] args) { using (var client = new HttpClient()) { try { // Specify the URL var request = new { name = "Warner Hope", age = 32 }; var response = await client.PutAsJsonAsync("https://www.example.com/api/data/1", request);// Send the PUT request response.EnsureSuccessStatusCode();// Check the response var content = await response.Content.ReadAsStringAsync();// Read the response Console.WriteLine(content); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } } } }

Explanation:

In the above example, the PutAsJsonAsync method is used to send the PUT request. The method takes two arguments: the URL to send the request to, and the request content. The request content can be any object, which is then serialized to JSON and sent in the request body. The method returns a Task of type HttpResponseMessage, which represents the response from the server. The EnsureSuccessStatusCode method is used to check if the response was successful (returned with a status code in the 200-299 range), and if not, it throws an exception. The response content can be read as a string using the ReadAsStringAsync method.

C# HttpClient DELETE Request

The DeleteAsync method is used to send the DELETE request. The method takes one argument: the URL to send the request to.

The following example shows how you can use C# HttpClient to send a DELETE request:

using System; using System.Net.Http; using System.Threading.Tasks; namespace HttpClientExample { class Program { static async Task Main(string[] args) { using (var client = new HttpClient()) { try { var url = "https://www.example.com/api/data/1"; // Specify the URL var response = await client.DeleteAsync(url); // Send the DELETE request. response.EnsureSuccessStatusCode();// Check the response var content = await response.Content.ReadAsStringAsync(); // Read the response Console.WriteLine(content); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } } } }

Explanation:

In the above example, the DeleteAsync method is used to send the DELETE request. The method takes one argument: the URL to send the request to. The method returns a Task of type HttpResponseMessage, which represents the response from the server. The EnsureSuccessStatusCode method is used to check if the response was successful (returned with a status code in the 200-299 range), and if not, it throws an exception. The response content can be read as a string using the ReadAsStringAsync method.