We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
Hail-Kang
5 years agoNew member | Level 2
dl=1 option error
Hi,
Until recently, I downloaded a file using the dl=1 parameter in my C++ code.
ex) https://www.dropbox.com/{private_url}?dl=1
But now it doesn't work.
If use Python's requests module with the same URL, it will work.
And when you call another URL(not dropbox) with the corresponding C++ code, it is worked.
This is my code.
#include <iostream>
#include <Windows.h>
#ifndef DEFINE_WIN_HTTP
#define DEFINE_WIN_HTTP 1
#include <Winhttp.h>
#pragma comment (lib, "winhttp.lib")
#endif
#define BUFFER_SIZE 4096
int main()
{
WCHAR szUrl[] = L"https://www.dropbox.com/{private_url}?dl=1";
BYTE response[4096];
HINTERNET hSession, hConnect, hRequest;
URL_COMPONENTS urlComponents;
WCHAR szHostName[256], szUrlPath[2048];
DWORD dwSize;
DWORD dwStatusCode;
hSession = WinHttpOpen(L"sample", WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (hSession == NULL)
return -1;
ZeroMemory(&urlComponents, sizeof(URL_COMPONENTS));
urlComponents.dwStructSize = sizeof(URL_COMPONENTS);
urlComponents.lpszHostName = szHostName;
urlComponents.dwHostNameLength = sizeof(szHostName) / sizeof(WCHAR);
urlComponents.lpszUrlPath = szUrlPath;
urlComponents.dwUrlPathLength = sizeof(szUrlPath) / sizeof(WCHAR);
if (!WinHttpCrackUrl(szUrl, lstrlenW(szUrl), 0, &urlComponents)) {
WinHttpCloseHandle(hSession);
return -2;
}
hConnect = WinHttpConnect(hSession, szHostName, INTERNET_DEFAULT_PORT, 0);
if (hConnect == NULL) {
WinHttpCloseHandle(hSession);
return -3;
}
hRequest = WinHttpOpenRequest(hConnect, L"GET", szUrlPath, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
if (hRequest == NULL) {
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return -4;
}
if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0)) {
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return -5;
}
WinHttpReceiveResponse(hRequest, NULL);
dwSize = sizeof(DWORD);
WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, &dwStatusCode, &dwSize, WINHTTP_NO_HEADER_INDEX);
if (dwStatusCode == HTTP_STATUS_OK)
{
if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
printf("Error %u in WinHttpQueryDataAvailable.\n",
GetLastError());
ZeroMemory(response, dwSize + 1);
WinHttpReadData(hRequest, response, sizeof(BYTE) * BUFFER_SIZE, NULL);
}
else {
TCHAR szBuf[256];
wsprintf(szBuf, TEXT("Status Code %d"), dwStatusCode);
}
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
std::string response_string = std::string((char*)response);
std::cout << response_string;
return 0;
}
1 Reply
- Greg-DB5 years ago
Dropbox Community Moderator
Can you elaborate on what you mean when you say "it doesn't work"? What unexpected error or output are you getting?
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!