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;
}