Getting burnt with HttpClient

  • Spicing up downloading huge files by using HttpClient’s default timeout.
  • The uninvited guest while downloading huge files: OutOfMemoryException.
  • Is HttpCompletionOption.ResponseHeadersRead your friend or your enemy?
Please go ahead, release it to production. Source: http://i2.kym-cdn.com/entries/icons/original/000/000/043/disaster-girl.jpg

Spicing up downloading huge files by using HttpClient’s default timeout.

The bad news: I didn’t inivite OutOfMemoryException to the party. Oh wait. You just did.

Streaming comes to the rescue

The mighty HttpCompletionOption parameter

Is HttpCompletionOption.ResponseHeadersRead your friend or your enemy?

Learnings:

  • Defaults just like the timeout in this case are there for a reason. Try to find the reason behind them.
  • Timeouts are also important and make sure you know what you do when you set it to infinite. Even if you do, have some seconds thoughts about it and remove the infinite timeout. What’s better? Clogging your whole HTTP requesting pipeline time to time, because of some files downloading with 0 KB/s or keeping your pipeline responsive and accepting the fact that some files might not be downloaded or only after a few retry?
  • ServicePointManager.DefaultConnectionLimit is something that I’ve only found by doing my homework, which reminded me that some APIs still have a learning curve, which you may or may not skip, but there’s a chance that your customer will pay the price.
  • Know the context. Just take my file downloading example. It’s essential to know how large are they. Are you saving them on disk. What’s your CDN’s performance and so on … With this information in mind, you’re chances are higher for getting the calibration of the timeouts and the default connection limit right.
  • Experiment with the given frameworks and APIs to find their strengths and weaknesses in time.
  • Lookout for IDisposable return types (there are exceptions to it of course: https://stackoverflow.com/questions/15705092/do-httpclient-and-httpclienthandler-have-to-be-disposed)

--

--

--

Happy coding ;)

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Unity Fundamentals — Creating glass Material

Lossless Team +1:  Meet Justas, our new Technical Copywriter

Week 7: Coding bootcamp vs self teaching?

Once a Maker, Always a Maker: 2018 Community Review

Implementing Scrum at 2359 Media

Real Flutter interview questions

Understanding Stackdriver Audit Logs

Activity tab in the Google Cloud console

Host your own Jupyter Notebook Server

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Szántó Balázs

Szántó Balázs

Happy coding ;)

More from Medium

【C#】Behavioral Patterns Observer Mode

Global Using Directive in C# 10 with net 6.0

Resolve mixed content SSL error with Apache Virtual Hosts — .NET 6.0

C# Mock different HttpClient calls in the same method