How to build a web based user interaction layer in C#
With the ubiquity of web browsers, it can be ideal for the user interaction layer of applications to be web based. The most common approaches to building web based applications is to write server side scripts running on web servers. However, these approaches require server programs to be present in the production environment.
What if you want the web server functionality to be contained in your C# program? In C#, there is a System.Net.HttpListener
class which listens for HTTP requests from clients.
This post is part 1 of the sequel. In this post, I will introduce the HttpListener
class and how we can use it to receive HTTP requests from clients in our C# program.
Accept incoming HTTP request with HttpListener
To enable your C# program to have a web based user interaction layer, you will need to specify at least one url for HTTP clients to connect to your C# program.
// Create an instance of the httpListener HttpListener httpListener = new HttpListener(); // Add a prefix/url that the httpListener will listen to httpListener.Prefixes.Add("http://*:12345/"); // Start the http listener httpListener.Start();
That will get your HttpListener
ready in receiving HTTP requests from clients with any hostname. If you want to limit access by the hostname, replace * with the hostname that your HttpListener
allow. For instance, if you only want clients to access your HttpListener
via localhost, use the following code instead:
// Create an instance of the httpListener HttpListener httpListener = new HttpListener(); // Add a prefix/url that the httpListener will listen to httpListener.Prefixes.Add("http://localhost:12345/"); // Start the http listener httpListener.Start();
When a HTTP client sends a HTTP request to your HttpListener
, an instance of the System.Net.HttpListenerContext
class will be created. We can use the GetContext
method of the HttpListener
to get the HttpListenerContext
instance whenever a client sends a HTTP request to one of the prefixes that our HttpListener
listens to.
// Get the HttpListenerContext when there is // an available HTTP request from the client. // This code will block until a HTTP request // is received via one of the prefixes. HttpListenerContext context = httpListener.GetContext();
With the HttpListenerContext
instance, your C# program can analyse the HTTP request from the client and create a HTTP response to send back to the client.
Next in the line
This concludes my discussion on giving your C# program the capability to accept HTTP request from clients. Where to go next?
Part 2 discusses how we can process the HTTP request using the facilities provided by the System.Net.HttpListenerRequest
class.
Part 3 discusses how we can prepare a HTTP response using the facilities provided by the System.Net.HttpListenerResponse
class to send back to the client.
Part 4 concludes the sequel by discussing how to implement a simple mechanism to interact with the user with the concepts discussed in the first 3 parts.
Related posts
To digress, the following is a list of posts that relates to sending HTTP requests to web servers. Feel free to look through them as well. 🙂