main.cs1104 chars37 lines
In the above code, we first define a url
for our server which listens on http://localhost:8080/
. We then create an HttpListener
object and add the prefix to the listener using listener.Prefixes.Add(url)
.
After starting the listener using listener.Start()
, we enter into an infinite loop to keep listening for incoming HTTP requests.
For each incoming request, we use listener.GetContext()
to get the context of the request which contains both the HttpListenerRequest
and HttpListenerResponse
objects.
We then check if the request is a GET request using if (request.HttpMethod == "GET")
and prepare our response accordingly. In this case, we set the response content to "Hello, World!" and send it back to the client.
Finally, we close the output stream using output.Close()
.
gistlibby LogSnag