Brent says to consider the following if you are using this code to implement a file updater.
I want to point out that using the last modified date of a file would be a better indicator of a file's freshness than its size. The size may not change between updates, especially if an update involves correcting typos in text (the "teh" for "the" variety) or changing BMP files.
But comparing the date returned by the server to the local date and time doesn't always ensure problem-free updating because there is no guarantee that the local date and time are accurate. And there's no guarantee that the server is accurate, for that matter. It would be most appropriate to compare server time to server time between updates, thus requiring the storing of the most recently retrieved file date. But if it is the first update check, the only option is to use the local time for comparison.
Modifications
Here are the modifications to my demo to query the file date, instead of its length:
---[ FIND ]---5AsULong, _ 'HTTP_QUERY_CONTENT_LENGTH---[ REPLACE WITH ]---11AsULong, _ 'HTTP_QUERY_LAST_MODIFIED---[ FIND ]---Print Buffer$; " Bytes"---[ REPLACE WITH ]---Print Buffer$
Demo
Text here.
'Retrieving HTTP Content-length Demo'By Brent D. Thorn, 8/2005
remoteFile$ ="http://babek.info/libertybasicfiles/images/lbfor2.gif"
BUFFER.SIZE =32'size of buffer to hold length stringOpen"wininet"ForDLLAs#inet
'It would be a good idea to test for an Internet'connection before proceding. This demo assumes'you have one already.' Register a new user agent. Use proxy settings' set up in Internet Options Control Panel.CallDLL#inet,"InternetOpenA", _
"My User Agent"AsPtr, _
0AsLong, _ 'INTERNET_OPEN_TYPE_PRECONFIG
_NULL AsLong, _
_NULL AsLong, _
0AsULong, _
hInet AsULongIf hInet =0Then[ErrExit]' Open a request to a remote file. This does not' download the file, but simply requests its' headers, one of which should be "Content-length."Struct pdw, Context AsULongCallDLL#inet,"InternetOpenUrlA", _
hInet AsULong, _
remoteFile$ AsPtr, _
_NULL AsLong, _
0AsLong, _
0AsULong, _
pdw AsStruct, _
hRequest AsULongIf hRequest =0Then[ErrExit]' Query Content-length from the headers and copy' the number of bytes to a string buffer. One could' call this function with an empty buffer and it' will fill pdw.BufferLength.struct with the required' buffer size.Struct pdw, BufferLength AsULong
pdw.BufferLength.struct= BUFFER.SIZE
Buffer$ =Space$(BUFFER.SIZE)+Chr$(0)Struct pdw2, Index AsULong
pdw2.Index.struct=0CallDLL#inet,"HttpQueryInfoA", _
hRequest AsULong, _
5AsULong, _ 'HTTP_QUERY_CONTENT_LENGTH
Buffer$ AsPtr, _
pdw AsStruct, _
pdw2 AsStruct, _
ret AsLongIf ret =0Then[ErrExit]
Buffer$ =Left$(Buffer$, pdw.BufferLength.struct)Print Buffer$; " Bytes"[ErrExit]' If you get an error, find GetLastError on MSDN. There' you will find a link to a list of error codes.CallDLL#kernel32,"GetLastError", ret AsULongIf ret ThenPrint"Error ";ret
' Free the handles we created and exit.If hRequest Then _
CallDLL#inet,"InternetCloseHandle", _
hRequest AsULong, ret AsLongIf hInet Then _
CallDLL#inet,"InternetCloseHandle", _
hInet AsULong, ret AsLongClose#inet
End
Length of a File on the Internet
-Originally published in NL 136
Length of a File on the Internet | Notes from Brent | Modifications | Demo
Notes from Brent
Brent says to consider the following if you are using this code to implement a file updater.
I want to point out that using the last modified date of a file would be a better indicator of a file's freshness than its size. The size may not change between updates, especially if an update involves correcting typos in text (the "teh" for "the" variety) or changing BMP files.
But comparing the date returned by the server to the local date and time doesn't always ensure problem-free updating because there is no guarantee that the local date and time are accurate. And there's no guarantee that the server is accurate, for that matter. It would be most appropriate to compare server time to server time between updates, thus requiring the storing of the most recently retrieved file date. But if it is the first update check, the only option is to use the local time for comparison.
Modifications
Here are the modifications to my demo to query the file date, instead of its length:Demo
Text here.Length of a File on the Internet | Notes from Brent | Modifications | Demo