make a http request with a custom body as binary in csharp

To make a HTTP request with a binary custom body in c#, you can use the HttpClient class provided by the System.Net.Http package.

First, you need to create an instance of HttpClient:

main.cs
HttpClient httpClient = new HttpClient();
42 chars
2 lines

Then, you can create an instance of ByteArrayContent for your custom binary body:

main.cs
byte[] requestBody = new byte[] { 0x01, 0x02, 0x03 };
ByteArrayContent content = new ByteArrayContent(requestBody);
116 chars
3 lines

Finally, you can make the HTTP request using the PostAsync method of the HttpClient instance:

main.cs
HttpResponseMessage response = await httpClient.PostAsync("https://example.com/api", content);
string responseData = await response.Content.ReadAsStringAsync();
161 chars
3 lines

The above code will send a HTTP POST request to https://example.com/api with the binary body represented by the byte array requestBody. The response will be stored in the response variable, and its content can be read as a string using the ReadAsStringAsync method.

gistlibby LogSnag