Download file from the url.
-
We want to download a file from the url. If we are download a text file then this code will download the file successfully, but if we are trying to download a exe or even a jpg file then this code does not work fine. We are using following code - DWORD dwSize = 0; DWORD dwDownloaded = 0; LPSTR pszOutBuffer; BOOL bResults = FALSE; HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL; hSession = WinHttpOpen( L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); if (hSession) hConnect = WinHttpConnect(hSession, L"localhost.com", INTERNET_DEFAULT_HTTP_PORT, 0); if (hConnect) hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/CMS/upload/1.txt", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH); if (hRequest) bResults = WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0); if (bResults) bResults = WinHttpReceiveResponse( hRequest, NULL); if (bResults) { dwSize = 0; if (!WinHttpQueryDataAvailable( hRequest, &dwSize)) printf("Error %u in WinHttpQueryDataAvailable.\n", GetLastError()); if (dwSize > 0) { pszOutBuffer = new char[dwSize+1]; } if (!pszOutBuffer) { printf("Out of memory\n"); dwSize=0; } else { if (dwSize>0) { ZeroMemory(pszOutBuffer, dwSize+1); if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded)) printf("Error %u in WinHttpReadData.\n", GetLastError()); } } HANDLE hFile; DWORD dwNumWritten; BOOL bTest; ofstream outfile ("D:\\1.txt",ofstream::binary); outfile.write(pszOutBuffer,sizeof(dwSize) * 50); Please help me what I am doing wrong.
-
We want to download a file from the url. If we are download a text file then this code will download the file successfully, but if we are trying to download a exe or even a jpg file then this code does not work fine. We are using following code - DWORD dwSize = 0; DWORD dwDownloaded = 0; LPSTR pszOutBuffer; BOOL bResults = FALSE; HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL; hSession = WinHttpOpen( L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); if (hSession) hConnect = WinHttpConnect(hSession, L"localhost.com", INTERNET_DEFAULT_HTTP_PORT, 0); if (hConnect) hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/CMS/upload/1.txt", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH); if (hRequest) bResults = WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0); if (bResults) bResults = WinHttpReceiveResponse( hRequest, NULL); if (bResults) { dwSize = 0; if (!WinHttpQueryDataAvailable( hRequest, &dwSize)) printf("Error %u in WinHttpQueryDataAvailable.\n", GetLastError()); if (dwSize > 0) { pszOutBuffer = new char[dwSize+1]; } if (!pszOutBuffer) { printf("Out of memory\n"); dwSize=0; } else { if (dwSize>0) { ZeroMemory(pszOutBuffer, dwSize+1); if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded)) printf("Error %u in WinHttpReadData.\n", GetLastError()); } } HANDLE hFile; DWORD dwNumWritten; BOOL bTest; ofstream outfile ("D:\\1.txt",ofstream::binary); outfile.write(pszOutBuffer,sizeof(dwSize) * 50); Please help me what I am doing wrong.
You could use the
URLDownloadToFile
API.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
We want to download a file from the url. If we are download a text file then this code will download the file successfully, but if we are trying to download a exe or even a jpg file then this code does not work fine. We are using following code - DWORD dwSize = 0; DWORD dwDownloaded = 0; LPSTR pszOutBuffer; BOOL bResults = FALSE; HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL; hSession = WinHttpOpen( L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); if (hSession) hConnect = WinHttpConnect(hSession, L"localhost.com", INTERNET_DEFAULT_HTTP_PORT, 0); if (hConnect) hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/CMS/upload/1.txt", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH); if (hRequest) bResults = WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0); if (bResults) bResults = WinHttpReceiveResponse( hRequest, NULL); if (bResults) { dwSize = 0; if (!WinHttpQueryDataAvailable( hRequest, &dwSize)) printf("Error %u in WinHttpQueryDataAvailable.\n", GetLastError()); if (dwSize > 0) { pszOutBuffer = new char[dwSize+1]; } if (!pszOutBuffer) { printf("Out of memory\n"); dwSize=0; } else { if (dwSize>0) { ZeroMemory(pszOutBuffer, dwSize+1); if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded)) printf("Error %u in WinHttpReadData.\n", GetLastError()); } } HANDLE hFile; DWORD dwNumWritten; BOOL bTest; ofstream outfile ("D:\\1.txt",ofstream::binary); outfile.write(pszOutBuffer,sizeof(dwSize) * 50); Please help me what I am doing wrong.
avika wrote:
but if we are trying to download a exe or even a jpg file then this code does not work fine.
What do you mean exactly? BTW you should use the '
code block
' button to post code.avika wrote:
outfile.write(pszOutBuffer,sizeof(dwSize) * 50);
BTW2 Why
50
in the above line?If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
avika wrote:
but if we are trying to download a exe or even a jpg file then this code does not work fine.
What do you mean exactly? BTW you should use the '
code block
' button to post code.avika wrote:
outfile.write(pszOutBuffer,sizeof(dwSize) * 50);
BTW2 Why
50
in the above line?If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You could use the
URLDownloadToFile
API.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
if We are pass only outfile.write(pszOutBuffer,sizeof(dwSize)); then it show less data in the text file. But if We pass the outfile.write(pszOutBuffer,sizeof(dwSize) * 50); then it show complete data thats why we are passing this.
avika wrote:
outfile.write(pszOutBuffer,sizeof(dwSize) * 50);
That should be
outfile.write(pszOutBuffer,dwSize);
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I have tried the method which are suggest by u, Superman but it gives following error --- unresolved external symbol _URLDownloadToFileW@20 referenced in function "void __cdecl funDownloadFile(void)" (?funDownloadFile@@YAXXZ)
Add
Urlmon.lib
toProject Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies
.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
Add
Urlmon.lib
toProject Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies
.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
We want to download a file from the url. If we are download a text file then this code will download the file successfully, but if we are trying to download a exe or even a jpg file then this code does not work fine. We are using following code - DWORD dwSize = 0; DWORD dwDownloaded = 0; LPSTR pszOutBuffer; BOOL bResults = FALSE; HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL; hSession = WinHttpOpen( L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); if (hSession) hConnect = WinHttpConnect(hSession, L"localhost.com", INTERNET_DEFAULT_HTTP_PORT, 0); if (hConnect) hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/CMS/upload/1.txt", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH); if (hRequest) bResults = WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0); if (bResults) bResults = WinHttpReceiveResponse( hRequest, NULL); if (bResults) { dwSize = 0; if (!WinHttpQueryDataAvailable( hRequest, &dwSize)) printf("Error %u in WinHttpQueryDataAvailable.\n", GetLastError()); if (dwSize > 0) { pszOutBuffer = new char[dwSize+1]; } if (!pszOutBuffer) { printf("Out of memory\n"); dwSize=0; } else { if (dwSize>0) { ZeroMemory(pszOutBuffer, dwSize+1); if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded)) printf("Error %u in WinHttpReadData.\n", GetLastError()); } } HANDLE hFile; DWORD dwNumWritten; BOOL bTest; ofstream outfile ("D:\\1.txt",ofstream::binary); outfile.write(pszOutBuffer,sizeof(dwSize) * 50); Please help me what I am doing wrong.
This above code works for binary image download, but the problem is: For an image file with 18k size, only 4k can be download and just half of the image can be seen on the client side. Any ideas? Thanks.