Featured Article

.Net Framework HttpClient Example Tutorial With Code Snippets

Kenneth Jul 13, 2026

In the ever-evolving landscape of web development, .NET Framework has consistently provided robust solutions for building scalable and secure applications. One of its powerful libraries for making HTTP requests is HttpClient, which simplifies asynchronous communication with web services and APIs. Let's delve into an example that demonstrates the basic usage of HttpClient in .NET Framework.

Fix: Windows service hosting WCF service is not starting
Fix: Windows service hosting WCF service is not starting

Before we proceed, ensure that you've installed the necessary NuGet package, System.Net.Http, in your project. This package contains the HttpClient class and other related types.

How to download and install Microsoft’s .Net 3.5 Framework on Windows 11
How to download and install Microsoft’s .Net 3.5 Framework on Windows 11

Creating an Instance of HttpClient

The first step is to create an instance of the HttpClient class. HttpClient is designed to be used as a singleton to avoid any overhead of opening and closing connections. Therefore, you should reuse the same instance throughout your application.

Client Challenge
Client Challenge

Here's an example of how to create an instance of HttpClient:

Using HttpClient in a Console Application

Difference Between .NET and ASP.NET
Difference Between .NET and ASP.NET

In a console application, you can create an instance of HttpClient in the Main method or any other method where you're making HTTP requests:

    HttpClient client = new HttpClient();

Now, let's use this instance to send a GET request to a simple REST API like JSONPlaceholder to fetch a list of todo items.

Sending a GET Request with HttpClient

NET HELPMSG 3534: What Does It Mean & How to Fix It
NET HELPMSG 3534: What Does It Mean & How to Fix It

To send a GET request, call the GetAsync method of the HttpClient instance, passing the API URL as an argument. This method returns a Task<HttpResponseMessage>, which you can await to get the response.

    HttpResponseMessage response = await client.GetAsync("https://jsonplaceholder.typicode.com/todos");

Once you have the response, you can read the content as a string using the ReadAsStringAsync method:

    string content = await response.Content.ReadAsStringAsync();

Now, you can parse the response as JSON using Newtonsoft.Json or System.Text.Json libraries to work with the data:

MTE Explains: The Difference Between HTTP and HTTPS
MTE Explains: The Difference Between HTTP and HTTPS

Parsing JSON Response with System.Text.Json

First, install the System.Text.Json NuGet package. Then, define a C# class that matches the structure of the JSON response:

two computer screens with faces on them and the words htpp vs hpps
two computer screens with faces on them and the words htpp vs hpps
an image of a diagram showing the process to retrieve data from a web browser or server
an image of a diagram showing the process to retrieve data from a web browser or server
GL services to use VPN
GL services to use VPN
Site-to-Site Connection
Site-to-Site Connection
Complete List of Mac Keyboard Shortcuts + Free PDF
Complete List of Mac Keyboard Shortcuts + Free PDF
Getting Ready For HTTP2: A Guide For Web Designers And Developers — Smashing Magazine
Getting Ready For HTTP2: A Guide For Web Designers And Developers — Smashing Magazine
Nexfil in Termux – Username OSINT Tool
Nexfil in Termux – Username OSINT Tool
Creating a Web Form
Creating a Web Form
Web Basic Concepts ⚡🛜
Web Basic Concepts ⚡🛜

```csharp public class Todo { public int UserId { get; set; } public int Id { get; set; } public string Title { get; set; } public bool Completed { get; set; } } ```

Now, parse the JSON response using the JsonSerializer.Deserialize<IEnumerable<Todo>> method:

    IEnumerable<Todo> todos = JsonSerializer.Deserialize<IEnumerable<Todo>>(content);

Displaying the Results

Finally, you can display the results in the console window:

foreach (var todo in todos)
{
    Console.WriteLine($"{todo.Id}. {todo.Title} - Completed: {todo.Completed}");
}

This demonstrates the basic usage of HttpClient to send a GET request and parse the JSON response in a .NET Framework console application.

In conclusion, HttpClient is a powerful and convenient way to make HTTP requests in .NET Framework. By leveraging its capabilities, developers can create robust applications that communicate effectively with web services and APIs. Start exploring the full potential of HttpClient by diving into more complex scenarios like sending POST, PUT, and DELETE requests, handling errors, and working with custom headers.