TCP - Get URL File Size / Date

Category: Internet

Date: 02-16-2022

Return to Index


 
'Sometimes you simply want to know how big a file (on the server) is - you
'don't actually want to download it. That information is contained in the
'header of an HTTP response, which you can get by using the HEAD
'method in an HTTP request rather than the usual GET.
 
'One of the lines in the header will be a Name:Value pair like this,
'where the byte size follows the colon.
Content-Length: 36
 
 
 
'Primary Code:
'Here's the code to get the header.
Tcp Open "HTTPAT "http://www.garybeene.comAS #1 TIMEOUT 60000
Tcp Print #1, "HEAD  http://www.garybeene.com/files/gbsnippets.msg  HTTP/1.0"
Tcp Print #1, "" : Tcp Recv #1, 4096, Buffer$ : Tcp Close #1
'Now, run both of these to get to the
bSize$ = Remain$(Buffer$, "Content-Length:")
bSize$ = Extract$(bSize$, $crlf)
bSize$ = Remain$(Buffer$, "Last-Modified:")
bSize$ = Extract$(bSize$, $crlf)
 
 
'Here's the HTTP response you'd get from the code above - just the header lines.
HTTP/1.1 200 OK
Date: Thu, 01 Oct 2009 03:02:37 GMT
Server: Apache
Last-Modified: Thu, 01 Oct 2009 02:19:54 GMT
ETag: "b24f18a-24-474d648708680"
Accept-Ranges: bytes
Content-Length: 36
Connection: Close
Content-TypeText/plain
 
 
'Compilable Example:  (Jose Includes)
' Client TCP/IP example - retrieve a web page
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg as Dword
 
Function PBMain() As Long
   Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
   Control Add Button, hDlg, 100,"Push", 50,10,100,20
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
      Local Buffer$, bSize$
      Tcp Open "HTTPAt "www.garybeene.comAs #1 TimeOut 60000
      Tcp Print #1, "HEAD  http://www.garybeene.com/files/gbsnippets.msg  HTTP/1.0"
      Tcp Print #1, "" : Tcp Recv #1, 4096, Buffer$ : Tcp Close #1
      MsgBox Buffer$  'just to show the header (visually verify the byte size)
      bSize$ = Remain$(Buffer$, "Content-Length:")
      bSize$ = Extract$(bSize$, $CrLf)
      MsgBox bSize$   'byte
      bSize$ = Remain$(Buffer$, "Last-Modified:")
      bSize$ = Extract$(bSize$, $crlf)
      MsgBox bSize$   'date
   End If
End Function
 
'gbs_00379
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm