C# Simple Http Server
In this short tutorial I’ll explain how to make a simple Http Server using only C#. The server will be able to serve any page that contains client-side code (html and javascript).
Basic stuff
When a client requests a page of a website (let’s say index.html), the Http Server will start looking for that file. If the file is found, the server will read all the content and send it back to the client as a byte array. After this, the connection is closed.
The coding part
Before starting, you need to change your project’s profile to .NET Framework 4 (not .NET Framework 4 client profile). This can be done by going to Project->Project properties->Application.
Now, go to Solution Explorer->References->Add Reference->.NET and import System.Web .
Also make sure you have the following lines included in your project’s header:
1
2
3
4
using System;
using System.Net;
using System.IO;
using System.Text;
Creating the server is quite simple - we’ll use .NET’s HttpListener:
1
2
3
4
5
HttpListener server = new HttpListener(); // this is the http server
server.Prefixes.Add("http://127.0.0.1/"); //we set a listening address here (localhost)
server.Prefixes.Add("http://localhost/");
server.Start(); // and start the server
The server is made, now we need to identify incoming connections and serve the requested files. For each connection, we send only one file and because we might have multiple connections, we’ll need an endless loop to handle them one by one.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
while (true)
{
HttpListenerContext context = server.GetContext();
//context: provides access to httplistener's response
HttpListenerResponse response = context.Response;
//the response tells the server where to send the datas
string page = Directory.GetCurrentDirectory() + context.Request.Url.LocalPath;
//this will get the page requested by the browser
if (page == string.Empty) //if there's no page, we'll say it's index.html
page = "index.html";
TextReader tr = new StreamReader(page);
string msg = tr.ReadToEnd(); //getting the page's content
byte[] buffer = Encoding.UTF8.GetBytes(msg);
//then we transform it into a byte array
response.ContentLength64 = buffer.Length; // set up the messasge's length
Stream st = response.OutputStream; // here we create a stream to send the message
st.Write(buffer, 0, buffer.Length); // and this will send all the content to the browser
context.Response.Close(); // here we close the connection
}
Well, this is all, now all you need to do is to create some html pages and place them into your executable’s directory.
Then run the application and access
http://127.0.0.1/anyfile.html.
You should be able to see your file in the browser’s window.
The complete code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Net;
using System.IO;
using System.Text;
namespace test
{
class Program
{
static void Main(string[] args)
{
HttpListener server = new HttpListener();
server.Prefixes.Add("http://127.0.0.1/");
server.Prefixes.Add("http://localhost/");
server.Start();
Console.WriteLine("Listening...");
while (true)
{
HttpListenerContext context = server.GetContext();
HttpListenerResponse response = context.Response;
string page = Directory.GetCurrentDirectory() + context.Request.Url.LocalPath;
if (page == string.Empty)
page = "index.html";
TextReader tr = new StreamReader(page);
string msg = tr.ReadToEnd();
byte[] buffer = Encoding.UTF8.GetBytes(msg);
response.ContentLength64 = buffer.Length;
Stream st = response.OutputStream;
st.Write(buffer, 0, buffer.Length);
context.Response.Close();
}
}
}
}
Remember that this server isn’t able to parse server-side languages like PHP or ASP.NET, it just sends the file’s content to the browser. If you’re interested in embedding ASP.NET into a http server see this tutorial.