Skip to content

Instantly share code, notes, and snippets.

@banderson5144
Created February 13, 2022 15:40
Show Gist options
  • Save banderson5144/d7e52940a44c3734259139479b1e28d3 to your computer and use it in GitHub Desktop.
Save banderson5144/d7e52940a44c3734259139479b1e28d3 to your computer and use it in GitHub Desktop.
C# Simple HTTP Server
using System.Net;
using System.Text;
namespace csharp
{
class Program
{
static void Main(string[] args)
{
HttpListener server = new HttpListener();
//make sure you add the appropriate netsh urlacl
//netsh http add urlacl url=http://localhost:8008/ user=DOMAIN\User listen=yes
server.Prefixes.Add("http://localhost:8008/");
server.Start();
Console.WriteLine("Listening...");
while (true)
{
HttpListenerContext context = server.GetContext();
HttpListenerResponse response = context.Response;
response.ContentType = "text/plain";
byte[] buffer = Encoding.UTF8.GetBytes("Hello World");
response.ContentLength64 = buffer.Length;
Stream st = response.OutputStream;
st.Write(buffer, 0, buffer.Length);
context.Response.Close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment