Paul Fallon's WebLog

From Ireland: a little bit of this and a little bit of that

IIS6 and Windows 2003 (Part 5)

What is particularly interesting about HTTP.SYS is that it has an API and it can be used to write/roll your own HTTP endpoint. Nice!!

Currently there is only a C API, but with Whidbey you have access to a managed API to HTTP.SYS.

Here is a sample (via Don's blog) of the HTTPListener class that comes in Whidbey

using System;
using System.Net;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        HttpListener http = new HttpListener();
        string uri = string.Format("http://localhost/{0}/", args[0]);
        http.Prefixes.Add(uri);
        http.Start();

        while (true)
        {
            HttpListenerContext ctx = http.GetContext();
            ctx.Response.ContentType = "text/html";
            TextWriter writer = new StreamWriter(ctx.Response.OutputStream);
            writer.WriteLine("<html><body><b>It's now {0}</b><br/><i>{1}</i></body></html>",
                             DateTime.Now,
                             ctx.Request.Url);
            writer.Flush();
            writer.Close();
        }
    }
}

Details about for the C API can be found at http://msdn.microsoft.com/library/en-us/http/http/http_api_overview.asp?frame=true and details for the Whidbey API (aka the HTTPListener class) can be found at at http://msdn2.microsoft.com/library/34xswsd2.aspx.

Also, Aaron Skonnard has just published a very good article on Running ASPX without IIS. Its published on the lastest version of the MSDN magazine and covers HttpListener, as well as how to host ASPX in your own custom process.

 

Published Tuesday, November 23, 2004 7:24 PM by no1138
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker