I need to send async request to the server and get the information from the response stream. I'm using HttpClient.GetStreamAsync(), but the server response that POST should be used. Is there a similar method like PostStreamAsync()? Thank you.
If you want to use HttpClient
for streaming large data then you should not use PostAsync
cause message.Content.ReadAsStreamAsync
would read the entire stream into memory. Instead you can use the following code block.
var message = new HttpRequestMessage(HttpMethod.Post, "http://localhost:3100/api/test");
var response = await client.SendAsync(message, HttpCompletionOption.ResponseHeadersRead);
var stream = await response.Content.ReadAsStreamAsync();
The key thing here is the HttpCompletionOption.ResponseHeadersRead
option which tells the client not to read the entire content into memory.
Use HttpClient.PostAsync
and you can get the response stream via HttpResponseMessage.Content.ReadAsStreamAsync()
method.
var message = await client.PostAsync(url, content);
var stream = await message.Content.ReadAsStreamAsync();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With