Why do HttpClient.PostAsync and PutAsync dispose the content?

前端 未结 1 1974
野趣味
野趣味 2021-01-05 03:47

The behavior of the HttpClient.PostAsync method is to dispose of the provided HttpContent object.

There are many ways to get around this behavior including construct

相关标签:
1条回答
  • 2021-01-05 04:38

    I couldn't immediately find the implementation on referencesource but the WCF source contains it as well. The method you're looking for is DisposeRequestContent(HttpRequestMessage) and the accompanying comment says this:

    When a request completes, HttpClient disposes the request content so the user doesn't have to. This also ensures that a HttpContent object is only sent once using HttpClient (similar to HttpRequestMessages that can also be sent only once).

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

    Basically it's a safeguard to make sure you don't send the same response twice which they consider a bad/uncommon/discouraged use case.

    0 讨论(0)
提交回复
热议问题