Skip to main content
Email Password   helpLost your password?

Screenshot - screenshot2.jpg

Introduction

This article describes the usage of a small HTTP server library entirely written in C# that can be integrated and used in your own application. The purpose of this library is to provide developers with an easy to use and flexible way of generating HTTP responses on the fly based on the requested URI and/or the current state of the application.

Background

This library was developed by Hybrid GeoTools to act as an integrated web server to provide dynamic data to Google Earth. Although handling Google Earth's requests is a simple task at a first glance, it became apparent that not handling the HTTP protocol properly can cause unexpected results. For instance, not handling the Connect header properly caused Google Earth to reuse the connection while the server was actually waiting for a new connection. As a result, the request was not handled at all.

For this reason, the HybridDSP.Net library was developed. Currently, this library only contains the HTTPServer, but more services might be added in the future.

Design

The library exposes four classes and two interfaces:

Using the code

First of all, at least one request handler needs to be defined in order to be able to do anything.

class DateTimeHandler : IHTTPRequestHandler
{
    public void HandleRequest(HTTPServerRequest request, HTTPServerResponse response)
    {
        if (request.URI == "/")
        {
            /**
             * In this example we'll write the body into the
             * stream obtained by response.Send(). This will cause the
             * KeepAlive to be false since the size of the body is not
             * known when the response header is sent.
             **/
            response.ContentType = "text/html";
            using (Stream ostr = response.Send())
            using (TextWriter tw = new StreamWriter(ostr))
            {
                tw.WriteLine("<html>");
                tw.WriteLine("<head>");
                tw.WriteLine("<title>Date Time Server</title>");
                tw.WriteLine("<meta http-equiv=\"refresh\" content=\"2\">");
                tw.WriteLine("</header>");
                tw.WriteLine("<body>");
                tw.WriteLine(DateTime.Now.ToString());
                tw.WriteLine("</body>");
                tw.WriteLine("</html>");
            }
        }
        else
        {
            response.StatusAndReason = HTTPServerResponse.HTTPStatus.HTTP_NOT_FOUND;
            response.Send();
        }
    }
}

This handler will generate an HTML page that shows the current date and time and refreshes itself every two seconds. If the request is not for the root, it will return an HTTP NOT FOUND response.

Next, a request handler factory needs to be defined in order to use the just created request handler.

class RequestHandlerFactory : IHTTPRequestHandlerFactory
{
    public IHTTPRequestHandler CreateRequestHandler(HTTPServerRequest request)
    {
        return new DateTimeHandler();
    }
}

This factory will simply always instantiate a DateTimeHandler.

The last step is to hook the whole thing together. To do this one has to instantiate a factory and pass it to the contructor of the HTTPServer object. Then the server can simply be started.

static void Main(string[] args)
{
    RequestHandlerFactory factory = new RequestHandlerFactory();
    HTTPServer server = new HTTPServer(factory, 8080);
    server.Start();

    Console.Write("Press enter to abort.");
    Console.ReadKey();

    server.Stop();
}

Now, take your favorite web browser, browse to http://localhost:8080, and enjoy your very own integrated web server.

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralSimple clear practical Pin
Wayne Walter
21:43 31 Aug '09  
AnswerRe: Simple clear practical Pin
Mark Swaanenburg
21:58 31 Aug '09  
GeneralQuestion on IO Pin
Jim Crafton
5:04 14 Nov '07  
GeneralRe: Question on IO Pin
Mark Swaanenburg
5:16 14 Nov '07  
QuestionIPv6 problems Pin
Dorli
6:45 13 Nov '07  
AnswerRe: IPv6 problems Pin
Mark Swaanenburg
1:06 14 Nov '07  
QuestionWhy didn't you use the HttpListener Class Pin
GLLNS
4:14 12 Sep '07  
AnswerRe: Why didn't you use the HttpListener Class Pin
Mark Swaanenburg
4:33 12 Sep '07  
AnswerRe: Why didn't you use the HttpListener Class Pin
icetea94
10:38 3 Aug '09  
QuestionRe: Why didn't you use the HttpListener Class Pin
Mark Swaanenburg
21:56 31 Aug '09  
AnswerRe: Why didn't you use the HttpListener Class Pin
icetea94
2:39 1 Sep '09  
AnswerRe: Why didn't you use the HttpListener Class Pin
Mark Swaanenburg
2:47 1 Sep '09  
GeneralRe: Why didn't you use the HttpListener Class Pin
icetea94
3:07 1 Sep '09  
GeneralRe: Why didn't you use the HttpListener Class Pin
Mark Swaanenburg
3:18 1 Sep '09  
GeneralRe: Why didn't you use the HttpListener Class Pin
icetea94
5:38 1 Sep '09  
GeneralRe: Why didn't you use the HttpListener Class Pin
Mark Swaanenburg
21:24 1 Sep '09  


Last Updated 14 Nov 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009