I'm interested in optimizing the behavior of a client calling a REST API. In this API, a particular POST operation allows for the inclusion of a If-None-Match: *
header, which causes the server to report an HTTP 412 response if the item being uploaded already exists in the system. When used with Expect: 100-continue
, the server will respond with the 412 instead of sending a 100 response, as described in RFC 2616 §8.2.3. I'd like to detect this condition in the client, and avoid transmitting the body of the request in this case.
When working directly with HttpWebRequest
, this is fairly straightforward to implement. After calling GetRequestStream
(or GetRequestStreamAsync
), but before writing data to the resulting Stream
, the code simply needs to check the HaveResponse
property. If the value is true
, close the request stream and proceed to call GetResponseAsync
.
Now I'm working on transitioning to use HttpClient
instead of working with HttpWebRequest
directly (for improved support across multiple platforms). Currently I'm using a StreamContent
to represent the body of the request, and observing that the content of the stream is sent regardless of whether the server sent a 100 or 412 response. How can I perform a similar optimization to the one described above, using HttpClient
and HttpRequestMessage
instead of HttpWebRequest
?