HttpListener Series – 1 – Get It Listening   1 comment

The series index here.

After getting a grip of what HTTP is, we can start playing with the HttpListener class that was presented to the world in .Net 2.0.

HttpListener, provides a wrapper that manages the socket and TCP levels of communication (streaming, buffering and so on) and reads and sets the protocol parameters, i.e, method, headers, status, content, etc..

So, if you want to start your own web server, HttpListener is the best way to start with. Let’s implement the example of the previous post in this series.

Starting a HttpListener couldn’t be simpler.

Instanciate it:

var listener = new HttpListener(); //instanciate it
listener.Prefixes.Add("http://+/HLD/"); //tell it what to listen to

listener.Start();  // start it

while (true)
    var context = listener.GetContext();  // get the request

    Console.WriteLine("Request received for: " + context.Request.Url.AbsoluteUri);
   // answer the request
    context.Response.ContentType = "text/html";
    context.Response.StatusCode = 200;
    context.Response.StatusDescription = "OK";

    var writer = new StreamWriter(context.Response.OutputStream);
    writer.Write("<!DOCTYPE HTML><html><head><title>Http Listener demo.</title></head><body><b>Hello</b>, this is the <i>HttpListener</i> responding.</body></html>");

    context.Response.Close(); // end the response


  • In the prefix the“+” wildcard is used for the domain name. This means every domain coming into the machine in the indicated port. So, “localhost” locally, the machine name or network IP will be valid URLs.
  • As this is Http, the port 80 is implicit. If a different port, for instance 8080, is intended just set “http://+:8080/HLD”.
  • An HttpListenerException “Access is denied” exception might be thrown in the listener.Start() statement if the user running the process is not allowed to open the port. Check here.
  • Setting Response.ContentType is very important. Maybe not so in Internet Explorer that “reads” the content and tries to figure what’s in it. Try changing it to “text/plain” and you’ll find IE and Chrome don’t exactly display alike.
  • You must always close the response by calling context.Response.Close(), otherwise the client will never get the response and will, eventually, time out.

You can now go to any Http client, for instance, the internet browser of your choice, access “http://localhost/HLD/&#8221;.

So, this is just what IIS does in the most simplified way. Mostly reading the content from a file system stream, setting the MIME type (context.Reponse.ContentType) according to the file extension. Will get there in the next post of these series.


Posted 2012/07/04 by Bigsby in Hands On

One response to “HttpListener Series – 1 – Get It Listening

Subscribe to comments with RSS.

  1. Pingback: HttpListener Series « Bigsby Spot

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: