To create a HTTP server in C#, we can leverage the HttpListener class provided by .NET. Here's an example of how to create a simple HTTP server that listens on port 8080 and reads the request body as form-urlencoded.
main.cs1281 chars40 lines
In this example, we create an instance of HttpListener
and add a prefix to indicate which URLs we want to listen on. We then start the listener and loop indefinitely, waiting for incoming requests.
When we receive a request, we check if it's a POST request with the content type application/x-www-form-urlencoded
. If it is, we read the request body as a string and process the form data as needed.
After processing the request, we send a simple "Hello World!" response back to the client.
gistlibby LogSnag