∟GetStringAsync() Method Example Program

A tutorial example is provided on how to use GetStringAsync() method in the System.Net.Http.HttpClient class to send a GET request in a child thread as am asynchronous operation.

Here is my first tutorial example C# program, GetStringAsyncTest.cs, on asynchronous operation with GetStringAsync() method from the HttpClient class:

// GetStringAsyncTest.cs
// Copyright (c) 2016 HerongYang.com. All Rights Reserved.

using System;
using System.Net.Http;
using System.Threading.Tasks;
public class GetStringAsyncTest {
   static void Main() {
      string uri = "https://www.herongyang.com/Service/Hello_REST.php";
      HttpClient c = new HttpClient();
      Task<string> t = c.GetStringAsync(uri);
      string s = t.Result;
      Console.WriteLine(s);	
   }
}

If you compile and run it with .NET Framework 4.6.1 SDK, you will get:

C:\herong>set "NET=\windows\Microsoft.NET\Framework\v4.0.30319"
C:\herong>set "REF=C:\Program Files\Reference Assemblies\Microsoft
   \Framework\.NETFramework\v4.6.1"

C:\herong>%NET%\csc "/r:%REF%/System.Net.Http.dll"
   GetStringAsyncTest.cs
Microsoft (R) Visual C# Compiler version 4.6.1055.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

C:\herong>GetStringAsyncTest.exe
<Result>
   <Message>
      Hello from server - herongyang.com.
   </Message>
</Result>

Cool. The example program works! Please note that:

As you can see, we are not using the C# 5 Async feature yet in the example program. So we need to do more tests in next tutorials.

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

 Class Features

 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

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Archived Tutorials

 References

 Full Version in PDF/ePUB