Last active
April 2, 2024 17:13
-
-
Save warm-ice0x00/bbb7d900a9f041fd214c69b8f6d9e6f5 to your computer and use it in GitHub Desktop.
This file contains 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
public class Program { | |
private static async System.Threading.Tasks.Task Listen(System.Net.HttpListener listener) { | |
while (true) { | |
try { | |
System.Net.HttpListenerContext context = | |
await listener.GetContextAsync().ConfigureAwait(false); | |
System.Net.HttpListenerRequest request = context.Request; | |
System.Net.HttpListenerResponse response = context.Response; | |
string text = System.Web.HttpUtility.UrlDecode(request.Url.AbsolutePath).Substring(1); | |
System.Console.WriteLine(text); | |
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text); | |
response.ContentType = "text/plain"; | |
response.ContentEncoding = System.Text.Encoding.UTF8; | |
response.ContentLength64 = bytes.LongLength; | |
await response.OutputStream.WriteAsync(bytes); | |
response.Close(); | |
} catch (System.Exception exception) { | |
System.Console.WriteLine(exception.Message); | |
} | |
} | |
} | |
public static async System.Threading.Tasks.Task Main() { | |
System.Net.HttpListener listener = new(); | |
listener.Prefixes.Add("http://localhost/"); | |
listener.Start(); | |
System.Console.CancelKeyPress += delegate { | |
listener.Close(); | |
}; | |
System.Collections.Generic.IEnumerator<string> prefixes = listener.Prefixes.GetEnumerator(); | |
prefixes.MoveNext(); | |
System.Console.WriteLine("Listening for connections on " + prefixes.Current); | |
await Listen(listener); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment