Tutorial :Download a URL in C++



Question:

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?


Solution:1

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.


Solution:2

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.


Solution:3

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.


Solution:4

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


Solution:5

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
Previous
Next Post »