Writing an asynchronous HTTP server and client
This recipe shows how to create a simple asynchronous HTTP server.
Getting ready
To step through this recipe, you will need Visual Studio 2012. 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 2012. 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;
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(); string response = await responseMessage...