Why are HttpClient.PostAsync and PutAsync using content?

The behavior of the HttpClient.PostAsync method is to get rid of the provided HttpContent .

There are many ways to get around this behavior, including creating a new HttpContent for each call made on the client, or loading content into a stream and changing the pointer.

I wonder why calling this method automatically causes its IDisposable parameters to be deleted? As far as I know, this is not the usual behavior in .NET.

It is also worth noting that this behavior is also observed in PUT requests that are idempotent, so the premise that this behavior prevents sending information again does not seem to be correct.

+5
source share
1 answer

I could not immediately find the implementation on sourceource, but the WCF source also contains it. The method you are looking for is DisposeRequestContent(HttpRequestMessage) , and the accompanying comment says the following:

When the request completes, HttpClient places the contents of the request, so the user does not need this. It also ensures that the HttpContent object HttpContent dispatched only once using the HttpClient (similar to HttpRequestMessages , which can also be dispatched only once).

 HttpContent content = request.Content; if (content != null) { content.Dispose(); } 

In principle, this is a guarantee that you do not send the same answer twice, which they consider a bad / unusual / discouraged use case.

+6
source

Source: https://habr.com/ru/post/1201108/


All Articles