HttpListener example.

Using a Http Listener.

    internal class Program
    {
        private static void Main(string[] args)
        {
            HttpListener listener = HttpListener.Create(IPAddress.Any, 8089);
            listener.RequestReceived += OnRequest;
            listener.Start(5);
            Thread.Sleep(9000000);
        }

        private static void OnRequest(object sender, RequestEventArgs e)
        {
            e.Response.Connection.Type = ConnectionType.Close;
            byte[] buffer = Encoding.UTF8.GetBytes("<html><body>Hello wordl!</body></html>");
            e.Response.Body.Write(buffer, 0, buffer.Length);
        }
    }

Creating and using a HTTPS listener

    internal class Program
    {
        private static void Main(string[] args)
        {
            var certificate = new X509Certificate2("C:\\yourcert.dat", "yourpassword");

            // We do the cast since we want to specify UseClientCert
            var listener = HttpListener.Create(IPAddress.Any, 8080, certificate);
            listener.RequestReceived += OnRequest;
            listener.Start(5);

            Console.ReadLine();
        }

        private static void OnRequest(object sender, RequestEventArgs e)
        {
            // Write info to the buffer.
            byte[] buffer = Encoding.ASCII.GetBytes("Hello secure world");
            e.Response.Body.Write(buffer, 0, buffer.Length);

            // Makes server stop trying to process this request.
            e.IsHandled = true;
        }
    }

Last edited Feb 3, 2010 at 7:43 AM by jgauffin, version 2

Comments

dudu2012 Sep 12, 2012 at 2:47 PM 
Can someone tell me how to generate a X509Certificate for HttpListener with openssl, your any reply will high be appreciated! Thank you!

var certificate = new X509Certificate2("C:\\yourcert.dat", "yourpassword");
~~~~~~~~~~~

epicus Aug 9, 2011 at 3:34 AM 
Hey,

I tried to port it too VB and thats what I got:


Imports HttpServer
Imports System.Net
Imports System.Text

Module Main

Sub Main(ByVal args As String())
Dim listener As HttpServer.HttpListener = HttpServer.HttpListener.Create(IPAddress.Loopback, 8089)
AddHandler listener.RequestReceived, AddressOf OnRequest
listener.Start(5)
Console.ReadLine()
End Sub

Sub OnRequest(ByVal sender As Object, ByVal e As RequestEventArgs)
Dim buffer As Byte() = Encoding.UTF8.GetBytes("<html><body>Hello wordl!</body></html>")
e.Request.Body.Write(buffer, 0, buffer.Length)
End Sub
End Module


but sadly. its not working. what am I doing wrong?

alien_boy Apr 2, 2011 at 11:43 AM 
ok - using the HttpServer.v1.1.zip dll - got this to work...

BTW downloaded the source code release and have a look at the "tutorials" and "samples" folders from which this working version was extracted....

using System.IO;
using System.Net;
using System.Threading;
using HttpServer;
using HttpListener = HttpServer.HttpListener;

namespace WebServerTest
{
class Program
{
private static void Main(string[] args)
{
HttpListener listener = HttpListener.Create(IPAddress.Any, 8089);
listener.RequestReceived += OnRequest;
listener.Start(5);
Thread.Sleep(9000000);
}

private static void OnRequest(object sender, RequestEventArgs e)
{
IHttpClientContext context = (IHttpClientContext)sender;
IHttpRequest request = e.Request;
IHttpResponse response = request.CreateResponse(context);
StreamWriter writer = new StreamWriter(response.Body);
writer.WriteLine("Goodbye to you too!");
writer.Flush();
response.Send();
}
}
}

alien_boy Mar 31, 2011 at 6:06 PM 
ok i give up whats the magic incantation....

when i comment in any of the "e.response" lines in the OnRequest i get an error....

"....'HttpServer.RequestEventArgs' does not contain a definition for 'Response' and no extension method 'Response' accepting a first argument of type 'HttpServer.RequestEventArgs' could be found (are you missing a using directive or an assembly reference?)..."

using HttpServer.v1.1.zip dll, various c#'s (2.0, 3.5 and 4.0) & vs2010

builds and runs perfectly when commented out and can watch call being made to the OnRequest()

using System.Threading;
using HttpServer;

namespace DatabaseServer
{
public class Program
{
public static void Main(string[] args)
{
HttpListener listener = HttpListener.Create(System.Net.IPAddress.Any, 8089);
listener.RequestReceived += OnRequest;
listener.Start(5);
Thread.Sleep(9000000);
}

public static void OnRequest(object sender, RequestEventArgs e)
{
//e.Response.Connection.Type = ConnectionType.Close;
//byte[] buffer = Encoding.UTF8.GetBytes("<html><body>Hello wordl!</body></html>");
//e.Response.Body.Write(buffer, 0, buffer.Length);
}
}
}

BoydFerris Apr 6, 2010 at 8:34 PM 
The example here is for 2.0; currently, the download link on the main page is 1.1. Download the latest source and compile to get a working copy of 2.0.

infamousdutch Feb 12, 2010 at 4:47 PM 
I have been reviewing this project, and it appears your first example of above is broken in the new release. RequestEventArgs does not expose a response object or property. I really like what you have done here, but I would like to get a working example up and running before committing to this as my embedded web server of choice. Feel free to contact me.

Thanks