Created
February 13, 2022 15:40
-
-
Save banderson5144/d7e52940a44c3734259139479b1e28d3 to your computer and use it in GitHub Desktop.
C# Simple HTTP Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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