Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get File Size via HTTP

Status
Not open for further replies.

craigsboyd

IS-IT--Management
Nov 9, 2002
2,839
US
This is not foolproof, in that some (very few) webservers will not return the size to the HTTPQueryInfo call. Also, this does not check for the existence of the file you are looking for (see thread1253-904509), so what you may get back is the byte size of the error page the webserver returned rather than the file you were looking for. That having been said, you may find the code proves quite useful if you are trying to add progress indication (or any other functionality to your HTTP download code that would require you to know the size of the file to be downloaded before actually downloading it). I've taken a few shortcuts in the code as I am in a bit of a rush today, so please do not view it as a polished piece. It is intended only to show the basics of how this can be accomplished. Had I the time, I would explain the different functions and parameters in detail... maybe at a later date or perhaps another member will jump in and provide some polish and additional information. In any event, here is the code (you can cut-n-paste this into a prg and run it, as the file exists on my webserver)
Code:
LOCAL lcURL
lcURL = "[URL unfurl="true"]http://www.sweetpotatosoftware.com/httpget.zip"[/URL]
MESSAGEBOX(TRANSFORM(GetFileSizeViaHTPP(lcURL)) + " Bytes",64,"FILE SIZE OF: " + UPPER(JUSTFNAME(lcURL)))

***********************************
FUNCTION GetFileSizeViaHTPP(tcURL)
***********************************
#Define INTERNET_OPEN_TYPE_PRECONFIG 	0
#Define INTERNET_DEFAULT_HTTP_PORT 		80
#Define INTERNET_SERVICE_HTTP 			3

Local lnReturn, hInternet, hSession, hRequest, lnBufferLen, lnLength, lcHost, lcSource

Do DeclareAPIFuncs

hInternet = InternetOpen("Filesize Connection", INTERNET_OPEN_TYPE_PRECONFIG, Null, Null, 0)
If hInternet != 0
	*!* this is a shoddy way to parse an URL
	*!* if I had more time I would show how to do it using
	*!* the InternetCrackUrl API function in wininet
	*!* but I don't, so for now I won't :)
	lcHost = SUBSTR(tcURL,AT("://",tcURL) + 3)
	lcHost = Left(lcHost,AT("/",lcHost) - 1)
	hSession = InternetConnect(hInternet, lcHost, INTERNET_DEFAULT_HTTP_PORT, Null, Null, INTERNET_SERVICE_HTTP, 0, 0)
	If hSession != 0
		hRequest = HttpOpenRequest(hSession, "GET", SUBSTR(tcURL,AT("/",tcURL,3)), "HTTP/1.0", Null, Null, 0, 0)
		If hRequest != 0
			lnReturn = HttpSendRequest(hRequest, Null, 0, Null, 0)
			If lnReturn != 0
				lnBufferLen = 1024
				lnLength = Replicate(Chr(0),4)
				=HttpQueryInfo(hRequest, Bitor(0x5, 0x20000000), @lnLength, @lnBufferLen, Null)
				*!* As I say I'm in a rush
				lnReturn = Asc(Substr(lnLength, 1,1)) + ;
					Asc(Substr(lnLength, 2,1)) * 256 +;
					Asc(Substr(lnLength, 3,1)) * 65536 +;
					Asc(Substr(lnLength, 4,1)) * 16777216
			Endif
		Else
			lnReturn = 0
		Endif
	Else
		lnReturn = 0
	Endif
	InternetCloseHandle(hInternet)
	Clear Dlls
Else
	lnReturn = 0
Endif
RETURN lnReturn
ENDFUNC

***********************************
Procedure DeclareAPIFuncs
***********************************
	Declare Integer InternetCloseHandle ;
		IN WinInet.Dll ;
		INTEGER

	Declare Integer InternetOpen ;
		IN WININET.Dll ;
		STRING,;
		INTEGER,;
		STRING, String, Integer

	Declare Integer HttpOpenRequest ;
		IN WININET.Dll ;
		INTEGER hHTTPHandle,;
		STRING lpzReqMethod,;
		STRING lpzPage,;
		STRING lpzVersion,;
		STRING lpzReferer,;
		STRING lpzAcceptTypes,;
		INTEGER dwFlags,;
		INTEGER dwContextw

	Declare Integer HttpSendRequest    ;
		IN WININET.Dll ;
		INTEGER hHTTPHandle,;
		STRING lpzHeaders,;
		INTEGER cbHeaders,;
		STRING lpzPost,;
		INTEGER cbPost

	Declare Integer HttpQueryInfo ;
		IN WININET.Dll ;
		INTEGER hHTTPHandle,;
		INTEGER nType,;
		STRING @cHeaders,;
		INTEGER @cbHeaderSize,;
		STRING cNULL

	Declare Integer InternetConnect ;
		IN WININET.Dll ;
		INTEGER hIPHandle,;
		STRING lpzServer,;
		INTEGER dwPort, ;
		STRING lpzUserName,;
		STRING lpzPassword,;
		INTEGER dwServiceFlags,;
		INTEGER dwReserved,;
		INTEGER dwReserved

ENDPROC

boyd.gif

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top