Writing an asynchronous HTTP server and client
This recipe shows you how to create a simple asynchronous HTTP server.
Getting ready
To step through this recipe, you will need Visual Studio 2015. No other prerequisites are required. The source code for this recipe can be found at BookSamples\Chapter9\Recipe2
.
How to do it...
The following steps demonstrate how to create a simple asynchronous HTTP server:
Start Visual Studio 2015. Create a new C# console application project.
Add a reference to the
System.Net.Http
framework library.In the
Program.cs
file, add the followingusing
directives:using System; using System.IO; using System.Net; using System.Net.Http; using System.Threading.Tasks; using static System.Console;
Add the following code snippet below the
Main
method:static async Task GetResponseAsync(string url) { using (var client = new HttpClient()) { HttpResponseMessage responseMessage = await client.GetAsync(url); string responseHeaders = responseMessage.Headers.ToString(); ...