Skip to content

Instantly share code, notes, and snippets.

@warm-ice0x00
Last active April 2, 2024 17:13
Show Gist options
  • Save warm-ice0x00/bbb7d900a9f041fd214c69b8f6d9e6f5 to your computer and use it in GitHub Desktop.
Save warm-ice0x00/bbb7d900a9f041fd214c69b8f6d9e6f5 to your computer and use it in GitHub Desktop.
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