I download files from the Internet using InternetOpenUrl and InternetReadFile. How do I get the size of a file prior to downloading? When downloading manually from the Internet, you can see total size just as the download process begins, but how do I get that size?
Asked
Active
Viewed 768 times
2
-
What platform? What API did you use? – trojanfoe Mar 23 '15 at 10:23
-
WinAPI, Windows added to OP – Dan Mar 23 '15 at 10:25
-
I think this question has been answered previously: http://stackoverflow.com/questions/9165926/using-wininet-to-identify-total-file-size-before-downloading-it – juhist Mar 23 '15 at 10:26
1 Answers
4
According to MSDN you should use HttpQueryInfo
to get the size of a resource accessed with InternetOpenUrl
. The corresponding query flag is called HTTP_QUERY_CONTENT_LENGTH
.

Dmitry Grigoryev
- 3,156
- 1
- 25
- 53
-
Can I use HINTERNET returned by InternetOpenUrl? If so, why does it fail? DWORD size = 0; DWORD r1=256, r2=0; HttpQueryInfoA(hFile, HTTP_QUERY_CONTENT_LENGTH| HTTP_QUERY_FLAG_NUMBER, &size, &r1, &r2); – Dan Mar 23 '15 at 11:07
-
@Dan: A wild guess: You are passing a `DWORD` as your receive buffer, and yet you are telling the API, that it is 256 bytes in size. – IInspectable Mar 23 '15 at 11:36
-
That was left from previous try when I tried using char array as a buffer. Tried setting it now to 4 bytes, but still it fails for some reason. Although the download works fine. – Dan Mar 23 '15 at 11:38
-
@Dan: [HttpQueryInfo](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384238.aspx): *"Returns **TRUE** if successful, or **FALSE** otherwise. To get extended error information, call **[GetLastError](https://msdn.microsoft.com/en-us/library/windows/desktop/ms679360.aspx)**."* – IInspectable Mar 23 '15 at 11:43
-
Here is the error I get : ERROR_HTTP_HEADER_NOT_FOUND The requested header could not be located. – Dan Mar 23 '15 at 12:06
-
Try to open a handle for your URL with [HttpOpenRequest](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384233%28v=vs.85%29.aspx) using "GET" as verb. Although I don't see why it wouldn't work with your handle as it is. – Dmitry Grigoryev Mar 23 '15 at 15:24