Code archives/Networking/HTTP FileSize() and more

This code has been declared by its author to be Public Domain code.

Download source code

HTTP FileSize() and more by Yan2005
This was written in answer to a forum post.
I'm putting it here as it may be of use to others.
Print HTTPFileSize("http://www.google.co.uk/intl/en_uk/images/logo.gif") + " Bytes"

End


Function HTTPFileSize(url$)
	url$ = url$.Replace("http://", "")
	Local slashPos = url$.Find("/"), host$, file$
	
	If slashPos <> -1
		host$ = url$[..slashPos]
		file$ = url$[slashPos..]
	Else
		Return -1
	EndIf
	
	Local stream:TStream = OpenStream("tcp::" + host$)
	If Not stream Then Return -1
	
	stream.WriteLine "HEAD " + file$ + " HTTP/1.0"
	stream.WriteLine "Host: " + host$
	stream.WriteLine ""
	
	While Not Eof(stream)
		Local in$ = stream.ReadLine()

		If in$.Find("Content-Length:") <> -1
			stream.Close()
			Return Int(in$[in$.Find(":") + 1..].Trim())
		EndIf	
	Wend
	
	stream.Close()
	Return -1		
End Function

Comments

Yan2006
Meet more of the family...

Returns the HTTP header.


Download a file with a progress bar (not really useful for anything, more of an example).



Filax2008
Another version with progressive download :




Code Archives Forum