Jump to content
  • Advertisement
Sign in to follow this  
nuclear123

C/C++ to download file from URL

This topic is 2556 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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++

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
+1 for libCURL

Everything else mentioned is completely Windows specific. You or someone else might actually want to port your code someday...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!