tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

HttpClient streaming upload

15 Jan 2015 .NET, Network, Performance

The requirement seemed simple. Get a stream of data and upload it using HTTP to server. The stream might be possibly large so complete buffering is is not an option. Nothing special.

But I was banging my head for last day (actually two times half of the day) solving it. The problem was I had to (and wanted to) use HttpClient because that was what I was using in the rest of the code I have for talking to my API. And HttpClient was fighting a little.

I knew the HttpWebRequest can do that. You just need to set AllowWriteStreamBuffering and start writing to the stream. Although HttpClient uses HttpWebRequest underneath you can’t fiddle with it. After some failures to conquer HttpClient I pulled out big guns aka source code. Jumping here and there I discovered PrepareAndStartContentUpload method. To avoid buffering I needed to know ContentLength (which I didn’t, because the stream was really streamed to me) or set TransferEncodingChunked. Did that. Compiled. Tested. Runs fine.

It makes sense, you just need to know. :) Hope it helps somebody to get to the finish quicker.