13

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?

User1
  • 39,458
  • 69
  • 187
  • 265

6 Answers6

25

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.

Joe
  • 41,484
  • 20
  • 104
  • 125
Dave Gamble
  • 4,146
  • 23
  • 28
  • +1 for libCURL. It is built exactly for this. It's more of a straight C binding (there's a C++ wrapper; haven't used it). – Joe Jul 15 '09 at 03:12
  • @Joe do you have an example (in C) compiled with MinGW for windows? I need this same example for windows using MinGW. – programmer Jan 09 '15 at 08:24
  • Updated the link to "simple.c" which has moved. – Joe Jan 09 '15 at 12:54
4

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.

Gerald
  • 23,011
  • 10
  • 73
  • 102
3

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".

Michael Haephrati
  • 3,660
  • 1
  • 33
  • 56
2

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.

Community
  • 1
  • 1
chrish
  • 2,352
  • 1
  • 17
  • 32
2

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

Nick Dandoulakis
  • 42,588
  • 16
  • 104
  • 136
  • Thanks for the mention of URLOpenBlockingStream! I couldn't find any good documentation on it anywhere besides rudimentary stuff on MSDN, but I finally figured it out (see my answer). – Andrew Aug 22 '18 at 04:12
  • By the way, do you know the scope of what that supports, in terms of things like SSL, HTTP 2, cookies, sessions, and all that? – Andrew Aug 22 '18 at 04:13
  • @Andrew, hi. The docs say that `URLOpenBlockingStream` requires IE3, so it probably supports the same stuff. For example, see [IE3 encryption](https://en.wikipedia.org/wiki/Internet_Explorer_3#Encryption). I don't think `URLOpenBlockingStream` supports cookies and sessions. – Nick Dandoulakis Aug 22 '18 at 09:12
  • Haha IE3... But yeah, I kinda gathered that much from the documentation: the functions like `URL___` seem to be major abstractions of the rest of the "URL Monikers" API. – Andrew Aug 22 '18 at 14:42
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.

Andrew
  • 5,839
  • 1
  • 51
  • 72