TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# HttpClient Example: System.Net.Http

This C# example uses the HttpClient type to download a web page. It requires System.Net.Http and System.Threading.Tasks.

HttpClient. Files from the Internet must often be accessed.

This requires more time due to reduced locality. The .NET Framework provides a HttpClient class that makes downloading files on separate threads easier. It helps simplify syntax.

Based on:

.NET 4.5

Example. First, this example uses the async and await keywords. In Main, it starts a task and uses the DownloadPageAsync method, an async method, as the target. This method is started—it downloads a web page.

In DownloadPageAsync, we use three using-statements. This helps improve system resource usage. We use the await keyword twice. We first call GetAsync and then ReadAsStringAsync. And finally we display the result string.

UsingStrings

C# program that uses HttpClient

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) + "...");
	    }
	}
    }
}

Output

Downloading page...
<!DOCTYPE html>
<html lang="en" dir="ltr" class="c...

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—its "Async" method instead uses an object token.

WebClient

Therefore: If async and await are used in the program, the HttpClient is preferable—it gains compiler checking and improved syntax.

Summary. HttpClient provides powerful functionality with better syntax support for newer threading features. It supports the await keyword. It enables threaded downloads of Internet files with better compiler checking and code validation.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf