Tutorial :Download a URL in C++


I want to be able to download a URL in C++. Something as simple as:

  std::string s;  s=download("http://www.example.com/myfile.html");  

Ideally, this would include URLs like:

I was using asio in Boost, but it didn't really seem to have the code for handling protocols like ftp and https. Now I discovered QT has more what I need (http://doc.trolltech.com/2.3/network.html).

It's tempting to make the switch to Qt, but it seems a bit heavy and intersects a lot of Boost functionality. Is it worth learning yet another API (Qt) or can Boost do more than I think?


Not a direct answer, but you might like to consider libCURL, which is almost exactly what you describe.

There are sample applications here, and in particular this demonstrates how simple usage can be.


I wouldn't go to Qt just for the networking stuff, since it's really not all that spectacular; there are a lot of missing pieces. I'd only switch if you need the GUI stuff, for which it is top notch.

libCURL is pretty simple to use and more robust than the Qt stuff.


The Poco Project has classes for cross-platform HTTP and FTP (and a lot of other stuff). There is overlap with boost. I recently found this, but have not used it.


You can use the URLDownloadToFile or URLOpenBlockingStream, although cURL, libcurl are the proper tools for that kind of jobs.


I got it working without either libcurl nor WinSock: https://stackoverflow.com/a/51959694/1599699

Special thanks to Nick Dandoulakis for suggesting URLOpenBlockingStream! I like it.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »