For learning experience I wrote my own http server code, my prime focus is on beginners who want to write their own http server. In my code I am simply writing a html string on the browser however when I wrote this code a very obvious question came into my mind
that "Can I display Image on browser through this code ? ", well YES we can do this by simply reading the image bytes and writing it on to the output stream.
Steps to test this code
1-Run the application
2-Open your browser type http://localhost:2000/ and hit enter
2000 is my port number it can be different in your case
using System;
using System.Linq;
using System.Net;
using System.Threading;
namespace ConsoleApplication3
{
class
Program
{
public
static HttpListener
_list = new
HttpListener();
static
void Main(string[]
args)
{
_list.Prefixes.Add("http://localhost:2000/");
_list.Start()
Console.WriteLine("server
started");
Thread
th = new Thread(loadsite);
th.Start();
Console.Read();
}
protected
static void
loadsite()
{
while (true)
{
HttpListenerContext context = _list.GetContext();
byte[]
response = Encoding.UTF8.GetBytes("<html><body>hello
world</body></html>");
context.Response.OutputStream.Write(response, 0, response.Length);
context.Response.KeepAlive =
false;
context.Response.Close();
}
}
}