To return an HTML response from an HTTP server in C#, you can use the HttpListener
class in .NET. Here is an example that shows how to create a basic HTTP server that returns an HTML response:
main.cs1325 chars39 lines
In this example, we create an HttpListener
and add a prefix for the URL we want to listen on (http://localhost:8000/
). We then start listening for incoming requests and loop indefinitely. When a request is received, we generate the HTML content we want to return, convert it to a byte array, set the content type to text/html
, set the content length to the length of the byte array, write the byte array to the response stream, and close the response.
gistlibby LogSnag