Geeks With Blogs
Nitin Reddy
Since Windows XP Service Pack 2, there's been a buddy living in the system called Http.sys. IIS and SQL Server both interact with this kernel-mode HTTP listener to receive and response to HTTP requests.

Within the .NET framework, you can build your own application to interact with HTTP requests using the HttpListener class, which is simply managed code built over the Http.sys API.

The code to work with the HttpListener class is quite simple, as illustrated with this example:

System.Net.HttpListener newHttpListener = new System.Net.HttpListener();

System.Net.HttpListenerContext context = newHttpListener.GetContext();
byte[] byteArr = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello World!");
context.Response.OutputStream.Write(byteArr, 0, byteArr.Length);


After instantiation of the HttpListener, we specify the prefix with which we want to receive requests. To process requests originating locally, you can specify a URL with localhost. To receive all requests, you can instead use an asterisk (*). The port number can be specified if you want to listen to requests on a non-standard port (the standard port is 80). The application starts listening for requests on calling GetContent, which is a synchronous blocking method (you can use the asynchronous method instead, if that's what you need). We then examing the incoming request - I left out this step for simplicity - and send out the response by writing to the output stream of the Response object.

With the HttpListener class, handling HTTP requests independent of IIS is a no-brainer. You don't have to work with sockets or process HTTP protocol messages - all that heavy lifting is already done for you by the HttpListener class leaving you to focus on what really matters, the idea that will build a better tomorrow. Posted on Sunday, June 22, 2008 2:36 AM | Back to top

Comments on this post: Using the HTTP Listener Class

# re: Using the HTTP Listener Class
Requesting Gravatar...
Thanks. Just what I needed.
Left by Jared on Jul 15, 2009 12:04 PM

# re: Using the HTTP Listener Class
Requesting Gravatar...
Good article,my 1question is how can i route the request directly to the internet and send the reply to the client,actually i need to build web proxy server with filtering capabilities
Left by Sameh on Apr 06, 2010 4:36 AM

# re: Using the HTTP Listener Class
Requesting Gravatar...
thanks , helped me
Left by وظاذف شاغره on Mar 17, 2011 6:05 AM

Your comment:
 (will show your gravatar)

Copyright © Nitin Reddy Katkam | Powered by: