views:

340

answers:

5

I'm using C++ without .NET on Win32, how can I download an image over HTTP from a website without having to re-invent the wheel? Is there an API or library that provides a single function to do this?

http://mywebsite/file.imgext --> C:\path\to\dir\file.imgext

+2  A: 

Use Windows Http Services API.

Sunny
This requires me to open up a connection, communicate, download and write it to a file using a buffered stream. Doesn't there exist an easier way to do this? Other languages can do this using a single line.
TomWij
Rude behavior invokes rude responses: use another language then.
Sunny
Btw, it's not a language thing, it's a library ...
Sunny
Sorry, it's not my intention to sound rude. And yes, you're right about that. ^^
TomWij
+1  A: 

If starting a new process is ok, you could have a look at WGET. (And even if not, the sources are available; you can look there to see how it's been implemented.)

Pukku
Well, it is an quick solution but not efficient and it produces overhead.
TomWij
+3  A: 

You could use cURLpp

I havn't used it yet, but example20 looks like it could solve your problem.

David Klein
libcurl was tried first, resulting in crashes, unaware of a C++ version of it. Yes, it has the same style as the first code we had, doing it in 6 lines.
TomWij
Why did it 'result in crashes'? With how commonly-used the library is, I'd assume the bug's on your side not theirs.
Neil Williams
If you're using libcurl as a win32 DLL, you MUST use the CURLOPT_WRITEFUNCTION if you set CURLOPT_WRITEDATA - or you will experience crashes. ==> Brings me back to re-inventing the wheel.
TomWij
Marked this answer as the solution as it requires 6 lines (by directly writing to an ofstream) and I suppose it works cross-platform as it's newer. :-)
TomWij
+2  A: 

You could use the WinInet or WinHTTP classes in C++. These are native Win32 APIs the abstract some of the work of getting sending and receiving files from the Internet.

I've used WinInet with great success to do just what you're trying to do.

CLaRGe
This requires me to open up a connection, communicate, download and write it to a file using a buffered stream.
TomWij
Here is some sample code from Microsoft on using WinInet for a FTP transfer: http://support.microsoft.com/kb/q238273/
CLaRGe
Will keep it in mind if I need to write a client with FTP capabilities, thank you. Still this would require me to re-invent downloading a file over HTTP to my local hard disk.
TomWij
A: 

Using POCO for this now. :-)

TomWij