User1
User1

Reputation: 41163

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?

Upvotes: 13

Views: 57412

Answers (6)

Michael Haephrati
Michael Haephrati

Reputation: 4215

You can use URLDownloadToFile.

#include <Urlmon.h>
    HANDLE hr;
    hr=URLDownloadToFile(NULL, L"http://www.example.com/myfile.html",L"mylocalfile.html",BINDF_GETNEWESTVERSION,NULL);

According to MSDN, BINDF_GETNEWESTVERSION - is a "Value that indicates that the bind operation retrieves the newest version of the data or object available. In URL monikers, this flag maps to the WinInet flag, INTERNET_FLAG_RELOAD, which forces a download of the requested resource".

Upvotes: 4

Andrew
Andrew

Reputation: 1

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.

Upvotes: 1

Dave Gamble
Dave Gamble

Reputation: 4174

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.

Upvotes: 26

Nick Dandoulakis
Nick Dandoulakis

Reputation: 43110

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

Upvotes: 2

chrish
chrish

Reputation: 2432

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.

Upvotes: 2

Gerald
Gerald

Reputation: 23479

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.

Upvotes: 4

Related Questions