using System; using System.Net; class Program { static void Main(string[] args) { HttpListener listener = new HttpListener(); listener.Prefixes.Add("http://localhost:8080/"); listener.Start(); Console.WriteLine("Listening..."); // wait for incoming request HttpListenerContext context = listener.GetContext(); Console.WriteLine("Request received!"); // stop listening listener.Stop(); } }
using System; using System.Net; using System.Threading; class Program { static void Main(string[] args) { HttpListener listener = new HttpListener(); listener.Prefixes.Add("http://localhost:8080/"); listener.Start(); Console.WriteLine("Listening..."); // start a new thread to handle incoming requests Thread thread = new Thread(() => { while (listener.IsListening) { HttpListenerContext context = listener.GetContext(); // do some work with context } }); thread.Start(); // wait for user input to stop listening Console.ReadLine(); // stop listening and wait for thread to complete listener.Stop(); thread.Join(); } }In this example, an `HttpListener` object is created and started on the `http://localhost:8080/` URL prefix. Instead of waiting for an incoming request on the main thread, a new thread is created to handle incoming requests in a loop. The main thread waits for user input to stop listening, at which point it calls the `Stop` method to stop the listener. The thread handling incoming requests also exits once the listener has stopped. Package library: `System.Net.HttpListener` Note: `HttpListener` only works on Windows operating systems.