I’ve written in the past about HTTP, how to perform asynchronous HTTP requests efficiently and how to use WebSockets in various ways, but sometimes it pays to keep it simple. There’s certainly a growing need for more sophisticated libraries that handle HTTP requests and that do so in an asynchronous manner, allowing developers to write responsive applications more easily. On the other hand, such libraries tend to be more difficult to use in simple scenarios where I might not need asynchrony. What if I just need a console application to download a file from the Web? A sophisticated asynchronous programming model is often overkill and can add a lot of complexity. Also, responsiveness isn’t really an issue, although some form of progress reporting might still be important. It turns out that, in these situations, some older APIs can come in handy.

URLDownloadToCacheFile is one such API and comes courtesy of Internet Explorer. This function is ideally suited to console applications because it doesn’t imply any particular threading model. It blocks while the file is downloaded and provides progress reporting using a callback. I’m going to show you how to use it to write a console application to download a given HTTP resource, present progress and download the file. The idea is to produce an executable that I can run as follows:

#api #http

An API for Simple HTTP Requests
2.95 GEEK