Jump to content

Downloading a file using C++

Go to solution Solved by mariushm,

Oh come on man, seriously... please try entering "URLDownloadToFile C++ example" in Google and the first page should return at least a few full C or C# examples. Should be fairly easy to convert to C++.

 

I know, sorry about the "Google it" reply but you're really asking very basic questions as if you want someone to write the code for you.

 

Here's a  C example, right from the first results page: http://www.go4expert.com/articles/download-file-using-urldownloadtofile-c-t28721/

 

I personally wouldn't use that function to download a file because maybe I don't want to save to disk the file, maybe I want just to keep the downloaded content in a buffer, or maybe let's say I implement a sort of update system for my application and I basically download a compressed file and I decompress it on the fly as the download progresses.

If you only need to download from unencrypted http, you should learn how to work with Winsock.

Given a URL, is it possible to download a file to a specified location on the local computer? I prefer standard windows libraries, but if third-party libs are the best, easiest way forward, I'm open to them too...

 

Thanks!

Nothing to see here ;)

Link to comment
https://linustechtips.com/topic/610573-downloading-a-file-using-c/
Share on other sites

Link to post
Share on other sites

See URLDownloadToFile which I think it's part of Internet Explorer :  https://msdn.microsoft.com/en-us/library/ms775123(v=vs.85).aspx

 

Here's some example code: http://stackoverflow.com/questions/5184988/should-i-use-urldownloadtofile and http://www.cplusplus.com/forum/windows/107840/

The URLDownloadToFile if I remember correctly also uses the proxy servers configured in Internet Explorer, if there are any, so it makes life simple if your code will run somewhere behind company proxy or something like that.

 

Without any built in functions, you would use Winsock, create a socket, sent the HTTP request to the remote web server, wait for reply, then parse the headers that come back and then save the stream of bytes to disk.  Would work for http only and you'd have to request to get stuff uncompressed (servers may gzip or deflate the reply while sending it)...

 

See Winsock documentation here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms741394(v=vs.85).aspx

Here's a basic http connection and retrieve html file example (not verified, but seems right) : http://www.zedwood.com/article/cpp-winsock-basic-http-connection

You could modify it to parse the headers and dump the data bytes to disk.

Link to post
Share on other sites

59 minutes ago, mariushm said:

See URLDownloadToFile which I think it's part of Internet Explorer :  https://msdn.microsoft.com/en-us/library/ms775123(v=vs.85).aspx

 

Here's some example code: http://stackoverflow.com/questions/5184988/should-i-use-urldownloadtofile and http://www.cplusplus.com/forum/windows/107840/

The URLDownloadToFile if I remember correctly also uses the proxy servers configured in Internet Explorer, if there are any, so it makes life simple if your code will run somewhere behind company proxy or something like that.

 

Without any built in functions, you would use Winsock, create a socket, sent the HTTP request to the remote web server, wait for reply, then parse the headers that come back and then save the stream of bytes to disk.  Would work for http only and you'd have to request to get stuff uncompressed (servers may gzip or deflate the reply while sending it)...

 

See Winsock documentation here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms741394(v=vs.85).aspx

Here's a basic http connection and retrieve html file example (not verified, but seems right) : http://www.zedwood.com/article/cpp-winsock-basic-http-connection

You could modify it to parse the headers and dump the data bytes to disk.

I'll look into them - thanks!

Nothing to see here ;)

Link to post
Share on other sites

4 hours ago, mariushm said:

See URLDownloadToFile which I think it's part of Internet Explorer :  https://msdn.microsoft.com/en-us/library/ms775123(v=vs.85).aspx

 

Here's some example code: http://stackoverflow.com/questions/5184988/should-i-use-urldownloadtofile and http://www.cplusplus.com/forum/windows/107840/

The URLDownloadToFile if I remember correctly also uses the proxy servers configured in Internet Explorer, if there are any, so it makes life simple if your code will run somewhere behind company proxy or something like that.

 

Without any built in functions, you would use Winsock, create a socket, sent the HTTP request to the remote web server, wait for reply, then parse the headers that come back and then save the stream of bytes to disk.  Would work for http only and you'd have to request to get stuff uncompressed (servers may gzip or deflate the reply while sending it)...

 

See Winsock documentation here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms741394(v=vs.85).aspx

Here's a basic http connection and retrieve html file example (not verified, but seems right) : http://www.zedwood.com/article/cpp-winsock-basic-http-connection

You could modify it to parse the headers and dump the data bytes to disk.

URLDownloadToFile seems to suit me - but I don't understand any part of it. Google does not return any search result with a solid-explanation (the func. is only mentioned) . Even if there is code, it's illegible! Can you show me the simplest code snippet to download a file from a given URL (with comments if possible)?

 

Thanks!

Nothing to see here ;)

Link to post
Share on other sites

Oh come on man, seriously... please try entering "URLDownloadToFile C++ example" in Google and the first page should return at least a few full C or C# examples. Should be fairly easy to convert to C++.

 

I know, sorry about the "Google it" reply but you're really asking very basic questions as if you want someone to write the code for you.

 

Here's a  C example, right from the first results page: http://www.go4expert.com/articles/download-file-using-urldownloadtofile-c-t28721/

 

I personally wouldn't use that function to download a file because maybe I don't want to save to disk the file, maybe I want just to keep the downloaded content in a buffer, or maybe let's say I implement a sort of update system for my application and I basically download a compressed file and I decompress it on the fly as the download progresses.

If you only need to download from unencrypted http, you should learn how to work with Winsock.

Link to post
Share on other sites

You're in over your head if you cant figure out how to use that function. Its very simple. There are only 2 parameters that actually matter the rest are NULL and 0. Perhaps try reading the the docs on it. There are also plenty of examples when you google the function and many of them are very legible. 

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to post
Share on other sites

Read this from MSDN. Then read this from MSDN. Then go here to find the functions of built in libraries. Sorry about the "solve it for yourself" answer, but you really are asking very basic questions that show that you have not researched the problem on your own first, and show that the programs you are trying to write are above your skill level (which is fine, everyone starts somewhere, but you won't learn anything if you always have somebody else tell you what code to write). In general, if you have a code question, and you do not have your own code to show and ask for better ways or specific help, you're not going to get satisfactory answers to your questions.

ENCRYPTION IS NOT A CRIME

Link to post
Share on other sites

18 hours ago, mariushm said:

Oh come on man, seriously... please try entering "URLDownloadToFile C++ example" in Google and the first page should return at least a few full C or C# examples. Should be fairly easy to convert to C++.

 

I know, sorry about the "Google it" reply but you're really asking very basic questions as if you want someone to write the code for you.

 

Here's a  C example, right from the first results page: http://www.go4expert.com/articles/download-file-using-urldownloadtofile-c-t28721/

 

I personally wouldn't use that function to download a file because maybe I don't want to save to disk the file, maybe I want just to keep the downloaded content in a buffer, or maybe let's say I implement a sort of update system for my application and I basically download a compressed file and I decompress it on the fly as the download progresses.

If you only need to download from unencrypted http, you should learn how to work with Winsock.

I thought I needed extra header files, libs, dll's, etc. Now I realize that only #include<Windows.h> and the function itself is enough (or, is it?) but I've run into a linker problem - I've attached a screenshot of it. Any idea why? Need I manually add libs or DLLs?

 

Capture.JPG

 

Thanks!

Nothing to see here ;)

Link to post
Share on other sites

6 minutes ago, mariushm said:

As the documentation pages say, the function is part of URLMon.dll , and you need to add URLmon.lib in the project to work.

 

AGAIN Read the documentation and the examples I linked to.

Hey, sorry! Please bear with me - Very new to Windows API programming... :|

 

Anyway, I copy-pasted a snippet of code, and it built successfully.

 

Thanks for all your help! :)

Nothing to see here ;)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×