To create a HTTP server that can read binary data from the request body in C#, you can use the HttpListener
class present in the System.Net
namespace. Here's an example code snippet that demonstrates this:
main.cs1000 chars35 lines
In this example, HttpListener
is used to create an HTTP server that listens on http://localhost:8080/
. Whenever a request is received, the Context
property of the HttpListener
instance is used to obtain a HttpListenerContext
object that represents the request and response objects for the received HTTP request. The binary data in the request body can then be read from the input stream of the request object and stored in a byte array.
Note that the InputStream
property of the HttpListenerRequest
instance provides a stream that represents the binary data of the request body. This stream can be read using various .NET stream classes like MemoryStream
or FileStream
, depending on your requirements.
Finally, the binary data read from the request is logged to the console and an empty response with status code 200 is sent back to the client.
gistlibby LogSnag