C/C++ to download file from URL

Started by
7 comments, last by ewil 12 years, 4 months ago
Could anyone please explain what i should use if im just wanting to have my program download a program and store on the clients PC from a specific url? should i use
1.) WINsock?
2.) WINinet?
3.) WINHTTP?
....etc.


thanks :) btw im using C/C++

Advertisement
Another option is libCURL
You should use WinSock, because HTTP downloads are a very, very simple application of basic network protocol. It will be a positive learning experience to learn how to submit a properly formed request and store the resulting data.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

You should use WinSock, because HTTP downloads are a very, very simple application of basic network protocol.

Really? Handling HTTP 1.0 wasn't so bad, but in this day and age it is a small nightmare to handle robustly...

Does your 'basic' implementation take into account redirects, access authorisation, persistent connections, not to mention HTTPS? All of those may be required if you don't have intimate knowledge of the webserver in question.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]


[quote name='Promit' timestamp='1324100239' post='4894708']
You should use WinSock, because HTTP downloads are a very, very simple application of basic network protocol.

Really? Handling HTTP 1.0 wasn't so bad, but in this day and age it is a small nightmare to handle robustly...

Does your 'basic' implementation take into account redirects, access authorisation, persistent connections, not to mention HTTPS? All of those may be required if you don't have intimate knowledge of the webserver in question.
[/quote]

But are all of these realy required? I mean he isn't asking how to create a program that logs-in to a gmail account and download all e-mails in alphabetical order.
Maybe he just wants to create a small program that downloads some text file from a website for the sole purpose of learning, and in that case I agree Winsock is a good start.

But are all of these realy required? I mean he isn't asking how to create a program that logs-in to a gmail account and download all e-mails in alphabetical order.
Maybe he just wants to create a small program that downloads some text file from a website for the sole purpose of learning, and in that case I agree Winsock is a good start.

Sure. As I said, if you have intimate knowledge of the web server and file you want to download, then you can just do a naive GET, and ignore everything else.

For anything more general, you should really be using a tested-and-tried implementation. Although quite frankly, even CURL is deficient in this area - you still have to manually parse and handle common idioms like meta: refresh.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

I use Wininet, downloading files is pretty easy with that.
+1 for libCURL

Everything else mentioned is completely Windows specific. You or someone else might actually want to port your code someday...
Since it sounds like you're targeting Windows I suggest you use the URLDownloadToFile function from urlmon.lib:

[source lang="cpp"]
#include <windows.h>
#include <urlmon.h>

#pragma comment(lib, "urlmon.lib")

int main(int argc, char *argv[]) {
// download the putty executable to putty.exe in the working directory, error checking omitted
URLDownloadToFile(NULL, "http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe", "putty.exe", 0, NULL);
return 0;
}
[/source]

It's the simplest option on the Windows platform. WinSock is too low-level for your purposes and WinInet is nice but the above wraps its functionality in an even easier to use library.

This topic is closed to new replies.

Advertisement