Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download a URL in C++

Tags:

c++

sockets

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:

  • ftp://example.com/myfile.dat
  • file:///usr/home/myfile.dat
  • https://example.com/myfile.html

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?

like image 789
User1 Avatar asked Jul 15 '09 03:07

User1


1 Answers

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

like image 68
Michael Haephrati Avatar answered Oct 11 '22 19:10

Michael Haephrati