Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
Makhlouf
6 years agoExplorer | Level 3
Programmatically download a shared file using C++ Curl
Hi,
I developped an aplication in C++ that runs on Windows. I am trying to add a feauture in the program to download a shared file from my dropbox folder. I would like the downloading to go in back...
Greg-DB
Dropbox Community Moderator
6 years agoThere are a few things that I notice that you may want to address:
- You should get rid of the "dl=0" URL, since that's not expected to work. (It is supposed to return an HTML page, which isn't what you want.)
- You're attempting to use both 'URLDownloadToFile' and 'curl', but you only need one. You should probably just decide on one and remove the other. (Personally, I'd just use 'curl' since it's more configurable.)
- You're setting 'CURLOPT_HTTPPROXYTUNNEL', which tells curl to use a proxy defined by 'CURLOPT_PROXY', but you're not setting 'CURLOPT_PROXY' that I can see. You should probably get rid of 'CURLOPT_HTTPPROXYTUNNEL', or properly set 'CURLOPT_PROXY'.
- The 'CURL_COULDNT_RESOLVE_HOST' error seems to indicate a DNS issue looking up the Dropbox domain, but the Dropbox domains are currently resolving properly, so you may need to check on any issues with your DNS server. It's also possible this is related to your proxy configuration, as noted above.
- Your linked "version.dat" file appears to just contain 56 bytes that don't print out anything visible. If you're trying to print that out as text, that might be the "nothing" you're referring to. You may want to try this with a different file that contains some printable text first for clarity.
Makhlouf
6 years agoExplorer | Level 3
Dear Greg,
Thank you for your kind support. Here is what I did:
1- I got rid of "dl=0" URL.
2- I got ride of URLDownladToFile(). I will stick to curl library.
3- I got ride of curl_easy_setopt (curl,CURLOPT_HTTPPROXYTUNNEL,1) ;
4- I am not using any proxy.
5- I still receive (6) CURL_COULDNT_RESOLVE_HOST error.
6- When I say receive nothing, I mean the downloaded file size is zero byte.
7- I uploaded the cpp file and tried to downloaded it (1.45 KB). The downloaded file size is still zero byte.
Below is the new code with above changes:
#include "Stdafx.h"
#include "curl.h"
#include "Download.h"
static const char* FileURL =
"https://www.dropbox.com/s/4cn3neh4omhhzr5/Download.cpp?dl=1" ;
static size_t write_data (void *ptr,size_t size,size_t nmemb,void* stream)
{
size_t written = fwrite (ptr,size,nmemb,(FILE * ) stream) ;
return written ;
}
int DownloadFile ()
{
static char TargetFile[] = "E:\\Download\\DownloadCURL\\Download.cpp" ;
// Download the file using curl library into DownloadCURL folder
if (auto curl = curl_easy_init ()) {
auto fp = fopen (TargetFile,"wb") ;
curl_easy_setopt (curl,CURLOPT_URL,FileURL) ;
curl_easy_setopt (curl,CURLOPT_WRITEFUNCTION,write_data) ;
curl_easy_setopt (curl,CURLOPT_FOLLOWLOCATION,1) ;
curl_easy_setopt (curl,CURLOPT_FAILONERROR,1) ;
curl_easy_setopt (curl,CURLOPT_WRITEDATA,fp) ;
/* Perform the request, res will get the return code */
auto res = curl_easy_perform (curl) ;
fclose (fp) ;
curl_easy_cleanup (curl) ;
} /* if (auto curl = curl_easy_init ()) */
return true ;
} /* Download */
If everything else is correct, could it be that my curl library is not functioning properly?
I downloaded the source code for the library (version 7-67.0). There were many options to apply during the compilation. I once built it with OpenSSL support. Then removed the OpenSSL support following a post I read about Windows / OpenSSL compatibility.
So any suggestion regarding building the library will be much appreciated.
Thanks again for your help
- Greg-DB6 years ago
Dropbox Community Moderator
Thanks, in that case the empty file isn't surprising since the call is failing, with the 'CURL_COULDNT_RESOLVE_HOST' error. I did just try it myself, and the Dropbox domain is resolving properly for me.
That error indicates a DNS issue, so you'll need to troubleshoot your DNS configuration/server. I can't offer support for your DNS server though, or curl or OpenSSL, as those are made by third parties.
- Makhlouf6 years agoExplorer | Level 3
Dear Greg,
Thanks a lot for you help. I will investigate the curl library myself. Glad to hear it worked for you. This gives me hope to do it myself as well.
- Здравко6 years agoLegendary | Level 20
Hi Makhlouf,
Did you try: https://www.dropbox.com/s/9thz78bwfeykqra/version.dat?raw=1 in your code? :thinking:
- Makhlouf6 years agoExplorer | Level 3
Dear Здравко,
Yes, I tried it and got me same error (6) CURL_COUDNT_RESOLVE_HOST.
There is one observation to mention here.
If I comment the line: curl_easy_setopt (curl,CURLOPT_FOLLOWLOCATION,1) ; the function curl_easy_perform () returns 0 which means no error, but the file size is 0 bytes.
When I decomment the above line, curl_easy_perform () returns 6. and the file size is still 0 bytes.
- Здравко6 years agoLegendary | Level 20
I can confirm that your code works using either "raw=1" or "dl=1" endings. Seems you have network issues. That's it. ( Of course could be done some improvements in the code, like detect and automatically save in corresponding file, accordingly named file for example :wink: )
Good luck!
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!