How to determine the size of a file retrieved by http protocol
-
Hy everyone. My purpose is to download a range of bytes (i.e. from the beggining up to a certain point) from a file located by a given URL. I am trying to determine the remote file size, but so far I didn't succeded. I am using WinInet, InternetReadFile function to actually read the file. I can read it, but I would like to determine it's size prior of downloading the whole file. If anyone could show me another way tp do that, more simple than this please do so. THANKS.:)
-
Hy everyone. My purpose is to download a range of bytes (i.e. from the beggining up to a certain point) from a file located by a given URL. I am trying to determine the remote file size, but so far I didn't succeded. I am using WinInet, InternetReadFile function to actually read the file. I can read it, but I would like to determine it's size prior of downloading the whole file. If anyone could show me another way tp do that, more simple than this please do so. THANKS.:)
Try:
HttpQueryInfo(hResource, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
&dwContentLength, &dwSize, NULL)where hResource is the one you use when calling InternetReadFile, dwContentLength is a DWORD to return the file-size into and dwSize =
sizeof
(DWORD).
"though nothing will keep us together we can beat them for ever and ever" rechi
-
Try:
HttpQueryInfo(hResource, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
&dwContentLength, &dwSize, NULL)where hResource is the one you use when calling InternetReadFile, dwContentLength is a DWORD to return the file-size into and dwSize =
sizeof
(DWORD).
"though nothing will keep us together we can beat them for ever and ever" rechi
Thanx Rechi ;-) One extra question ? R U from Romania ? I tried that, but unfortunately i get: ERROR_HTTP_HEADER_NOT_FOUND ... At first i had this in mind that is to play with such functions like Internetqueryoption and stuff, but no matter how much I dig up those flags I didn't find anyone related to the whole file size, just related to sections ... THANKZ anyway :->
-
Thanx Rechi ;-) One extra question ? R U from Romania ? I tried that, but unfortunately i get: ERROR_HTTP_HEADER_NOT_FOUND ... At first i had this in mind that is to play with such functions like Internetqueryoption and stuff, but no matter how much I dig up those flags I didn't find anyone related to the whole file size, just related to sections ... THANKZ anyway :->
gecool wrote: ERROR_HTTP_HEADER_NOT_FOUND It's about the headers, take a look at this (old) fragment:
HINTERNET hResource=::HttpOpenRequest(hConnection, \_T("GET"), szRelativeFile, NULL, NULL, NULL, INTERNET\_FLAG\_KEEP\_CONNECTION, 0); if (hResource) { LPTSTR szHeaders=\_T("Accept: audio/x-aiff, application/octet-stream, " "application/x-msdownload, audio/basic, " "audio/midi, audio/mpeg, audio/wav, image/jpeg, image/gif, " "image/jpg, image/png, image/mng, image/bmp, text/plain, " "text/html, text/htm\\r\\n"); CString szAgentHeader; szAgentHeader.Format(\_T("User-Agent: %s/1.0\\r\\n"), (LPCTSTR)m\_szAppName); HttpAddRequestHeaders(hResource, szHeaders, \_tcslen(szHeaders), HTTP\_ADDREQ\_FLAG\_ADD\_IF\_NEW); HttpAddRequestHeaders(hResource, (LPCTSTR)szAgentHeader, szAgentHeader.GetLength(), HTTP\_ADDREQ\_FLAG\_ADD\_IF\_NEW); if (HttpSendRequest(hResource, NULL, 0, NULL, 0)) { DWORD dw=0, dwOut=0, i=0, n=0, dwContentLength, dwSize=sizeof(DWORD); CString szFileFromPath=CUpgrader::GetFileFromPath( szRelativeFile), filepath; if (!::HttpQueryInfo(hResource, HTTP\_QUERY\_CONTENT\_LENGTH | HTTP\_QUERY\_FLAG\_NUMBER, &dwContentLength, &dwSize, NULL)) dwContentLength=-1; // unknown
and check if something is missing in your code. It's from an update system i wrote a year ago, and it works like a dream :-> gecool wrote: R U from Romania ? Yeah! :-D
"though nothing will keep us together we can beat them for ever and ever" rechi
-
gecool wrote: ERROR_HTTP_HEADER_NOT_FOUND It's about the headers, take a look at this (old) fragment:
HINTERNET hResource=::HttpOpenRequest(hConnection, \_T("GET"), szRelativeFile, NULL, NULL, NULL, INTERNET\_FLAG\_KEEP\_CONNECTION, 0); if (hResource) { LPTSTR szHeaders=\_T("Accept: audio/x-aiff, application/octet-stream, " "application/x-msdownload, audio/basic, " "audio/midi, audio/mpeg, audio/wav, image/jpeg, image/gif, " "image/jpg, image/png, image/mng, image/bmp, text/plain, " "text/html, text/htm\\r\\n"); CString szAgentHeader; szAgentHeader.Format(\_T("User-Agent: %s/1.0\\r\\n"), (LPCTSTR)m\_szAppName); HttpAddRequestHeaders(hResource, szHeaders, \_tcslen(szHeaders), HTTP\_ADDREQ\_FLAG\_ADD\_IF\_NEW); HttpAddRequestHeaders(hResource, (LPCTSTR)szAgentHeader, szAgentHeader.GetLength(), HTTP\_ADDREQ\_FLAG\_ADD\_IF\_NEW); if (HttpSendRequest(hResource, NULL, 0, NULL, 0)) { DWORD dw=0, dwOut=0, i=0, n=0, dwContentLength, dwSize=sizeof(DWORD); CString szFileFromPath=CUpgrader::GetFileFromPath( szRelativeFile), filepath; if (!::HttpQueryInfo(hResource, HTTP\_QUERY\_CONTENT\_LENGTH | HTTP\_QUERY\_FLAG\_NUMBER, &dwContentLength, &dwSize, NULL)) dwContentLength=-1; // unknown
and check if something is missing in your code. It's from an update system i wrote a year ago, and it works like a dream :-> gecool wrote: R U from Romania ? Yeah! :-D
"though nothing will keep us together we can beat them for ever and ever" rechi