Home
C#
HttpClient Example: System.Net.Http
Updated Nov 17, 2022
Dot Net Perls
HttpClient. Files from the Internet must often be accessed. This requires more time due to reduced locality. During this time, a program can perform other tasks.
Class notes. In C# programs we can use the HttpClient class, which makes downloading files on separate threads easier. It helps simplify syntax.
To start, we use the async and await keywords. In Main, we start a task and use DownloadPageAsync, an async method, as the target. This method is started—it downloads a web page.
Tip In DownloadPageAsync, we use 3 using-statements. This helps improve system resource usage.
Detail We use the await keyword twice. We first call GetAsync and then ReadAsStringAsync. And finally we display the result string.
using
using System; using System.Net.Http; using System.Threading.Tasks; class Program { static void Main() { Task t = new Task(DownloadPageAsync); t.Start(); Console.WriteLine("Downloading page..."); Console.ReadLine(); } static async void DownloadPageAsync() { // ... Target page. string page = "http://en.wikipedia.org/"; // ... Use HttpClient. using (HttpClient client = new HttpClient()) using (HttpResponseMessage response = await client.GetAsync(page)) using (HttpContent content = response.Content) { // ... Read the string. string result = await content.ReadAsStringAsync(); // ... Display the result. if (result != null && result.Length >= 50) { Console.WriteLine(result.Substring(0, 50) + "..."); } } } }
Downloading page... <!DOCTYPE html> <html lang="en" dir="ltr" class="c...
Static HttpClient. A network connection uses limited system resources. These can become exhausted with the normal "using" pattern on HttpClient.
And A static HttpClient may work better in some programs. If you run out of system resources with HttpClient, try a static HttpClient.
Note The "using" statement should usually be used with types that implement IDisposable. But if this causes an error, it can be omitted.
using System; using System.Net.Http; using System.Threading.Tasks; class Program { static void Main() { // Run the task. Task.Run(new Action(DownloadPageAsync)); Console.ReadLine(); } static HttpClient _client = new HttpClient(); static async void DownloadPageAsync() { // Use static HttpClient to avoid exhausting system resources for network connections. var result = await _client.GetAsync("http://www.example.com/"); // Write status code. Console.WriteLine("STATUS CODE: " + result.StatusCode); } }
STATUS CODE: OK
A discussion. The functionality of HttpClient overlaps with WebClient. The syntax of these two types is different. WebClient does not currently support the async and await syntax.
Note With WebClient, its "Async" method uses an object token. This is more clumsy.
WebClient
Detail If async and await are used in the program, the HttpClient is preferable—it gains compiler checking and improved syntax.
A review. HttpClient provides powerful functionality with better syntax support for newer threading features. It supports the await keyword.
HttpClient enables threaded downloads of Internet files with better compiler checking and code validation. It requires newer versions of .NET.
async
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Nov 17, 2022 (simplify).
Home
Changes
© 2007-2025 Sam Allen