GetStringAsync() Method in HttpClient Class
A quick introduction is provided on the System.Net.Http.HttpClient class and System.Threading.Tasks.Task<T> class, which can be used to play with asynchronous operations.
To practice the Async feature of C# 5, we can use the System.Net.Http.HttpClient class.
Here is some basic information about the HttpClient class.
System.Net.Http.HttpClient is a .NET Framework class that
allows us to send HTTP requests to remote Web sites
and receive HTTP responses in asynchronous operations.
Here are some basic methods supported in the HttpClient class:
- HttpClient() - Method to construct a new HttpClient object.
- Dispose() - Method to release and dispose resources of this HttpClient object.
- GetAsync(String) - Method to send a GET request to the specified URI as an asynchronous operation.
It returns immediately with new running task: System.Threading.Tasks.Task<HttpResponseMessage>.
When the task is completed, you will get an object of HttpResponseMessage representing the entire
- GetStringAsync(String) - Method to send a GET request to the specified URI as an asynchronous operation.
It returns immediately with new running task: System.Threading.Tasks.Task<string>.
When the task is completed, you will get a string representing the HTTP response body.
- GetByteArrayAsync(String) - Method to send a GET request to the specified URI as an asynchronous operation.
It returns immediately with new running task: System.Threading.Tasks.Task<byte>.
When the task is completed, you will get a byte array representing the HTTP response body.
As you can see, these Get*Async() methods return a generic class object,
System.Threading.Tasks.Task<T>. So we need to learn some details of the Task<T> class.
System.Threading.Tasks.Task<T> a system class
representing an asynchronous operation that returns a result when the operation is completed.
Here are some basic properties and methods supported in the Task<T> class:
- Task<T>(Func<T>) - Method to construct a new Task<T> object
with the specified function, which should return a value of type T.
When a new Task<T> object is created, .NET Framework will start a new thread
to execute the specified function.
- Wait(time) - Method to wait for the specified time in milliseconds or until the task is completed.
If the task is completed within the wait time, it returns true. Otherwise, it returns false.
- Status - Property to get the status of this Task<T>.
Possible status code are: Created, Running, RanToCompletion, Canceled, etc.
- Result - Property to get the result, type T, of this Task<T>.
The get accessor will wait until the task is completed.
With these two classes, we can start to play with asynchronous operations with
GetStringAsync(String) method provided in the HttpClient class. See the next tutorial
Table of Contents
About This Book
Introduction of C# (C Sharp)
Data Type and Variables
Logical Expressions and Conditional Statements
Arrays and Loop Statements
Data Type Features
Floating-Point Data Types
Passing Parameters to Methods
Execution Environment Class
Visual C# 2010 Express Edition
C# Compiler and Intermediate Language
Compiling C# Source Code Files
MSBuild - Microsoft Build Engine
Memory Usages of Processes
Multithreading in C#
►Async Feature from C# 5
What Is Async Feature?
►GetStringAsync() Method in HttpClient Class
GetStringAsync() Method Example Program
Watching Asynchronous Operation Execution Status
"await" Expression and Child Thread
"await" Expression Thread Example
"async" Function Example
WPF - Windows Presentation Foundation
Partial Classes and Partial Methods
Full Version in PDF/ePUB