Below is an example of creating an HTTP server in C# that reads the request body as binary and returns a binary response:
main.cs1032 chars36 lines
In this example, we create an instance of HttpListener
, add a prefix to listen for incoming requests, and start the server. We then enter an infinite loop to handle incoming requests.
For each request, we get the request and response objects from the HttpListenerContext
. We create a MemoryStream
and copy the input stream of the request into the memory stream as binary. We can then do something with the binary request data.
We create a binary response by encoding a string as UTF8
and writing it to the output stream of the response object. We also set the content type to application/octet-stream
to indicate that the response is binary.
Finally, we close the output stream of the response object to indicate that we are done writing the response.
Note that this example does not handle errors or support multiple requests concurrently.
gistlibby LogSnag