Una delle classi che mi piacciono di piu' nel nuovo framework e' HttpListener

Con poche linee si codice si puo implementare un mini web server e mostrare pagine create dinamicamente:

using System;
using System.Net;
using System.Threading; 

namespace HttpListenerExample
{
    
class Program
    {
        
static void Main(string[] args)
        {
            HttpListener listener = 
new HttpListener();

            listener.Prefixes.Add("http://+:5555/httplistener/");
            listener.Start();

            
string uri = @"http://localhost:5555/httplistener/";
            System.Diagnostics.Process browser = 
new System.Diagnostics.Process();
            browser.StartInfo.FileName = "iexplore.exe";
            browser.StartInfo.Arguments = uri;
            browser.Start();

            HttpListenerContext context = listener.GetContext();
            context.Response.ContentType = "text/html";
            String body = "<html><body><h1>This is the HTML body</h1></body></html>";

            
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(body);
            context.Response.ContentLength64 = buffer.Length;
            System.IO.Stream output = context.Response.OutputStream;
            output.Write(buffer,0,buffer.Length);
            output.Close();
            
            Thread.Sleep(100);
            listener.Stop();
        }
    }
}

Funziona solo con Windows XP SP2 o Windows Server 2003

powered by IMHO 1.3