Grabbing a web based file

Previous topic - Next topic

spicypixel

Is there a way we can directly grab a file from a remote http location and download it to our app (to be saved). Useful for PNGs etc., but far better for map data. I can get html info from a php or html file using NETWEB commands but I want the actual byte or word data from a specific file. This was pretty straightforward in blitz using Streams and Banks to create a http_get function. But can't get my head around it in GLB.

This is the nonsense I have so far for testing.

Code (glbasic) Select

SYSTEMPOINTER TRUE

GLOBAL host$
host$ = "www.spicypixel.net"

GetWebFile ("index.php", "savedfilename_unused.php")

SHOWSCREEN
MOUSEWAIT
END


FUNCTION GetWebFile: srcfilename$, destfilename$
LOCAL init_tcp%, web_file%, msg$

init_tcp% = SOCK_INIT()
web_file% = SOCK_TCPCONNECT(host$, 80)

SOCK_TCPSEND (web_file%, "GET /" + srcfilename$ + " HTTP/1.1")
SOCK_TCPSEND (web_file%, "Host: " + host$)
SOCK_TCPSEND (web_file%, "User-Agent: Android App")
SOCK_TCPSEND (web_file%, "Accept: */*")
SOCK_TCPSEND (web_file%, "")

SOCK_RECV (web_file%, msg$, 1024)
DEBUG "MSG: " + msg$ + "\n"

SOCK_CLOSE(web_file%)
RETURN TRUE
ENDFUNCTION



Here is a working Blitz Basic Routine for anyone interested...
Code (glbasic) Select

Function GetWebFile (srcfilename$, destfilename$)
www = OpenTCPStream (host$, 80)
If www=0 Then Print "Failed to connect": Return 0

Cls
Print "Downloading '"+srcfilename$+"' from "+host$

get$="GET /" + srcfilename$ + " HTTP/1.1"
WriteLine www, get$ ; GET / gets default page...
WriteLine www, "Host: " + host$
WriteLine www, "User-Agent: Overlord Client"
WriteLine www, "Accept: */*"
WriteLine www, ""

; Find Number of bytes to read in header data
bytesToRead = 0
Repeat
header$ = ReadLine (www)
If Left (header$, 16) = "Content-Length: " Then bytesToRead = Right (header$, Len (header$) - 16)
Until header$ = "" Or (Eof (www))
If bytesToRead = 0 Then CloseTCPStream www: Return 0

save = WriteFile (destfilename$) ; Create new file to write downloaded bytes into
If save = 0 Then CloseTCPStream www: Return 0

tempstorage=CreateBank(bytesToRead)
ReadBytes tempstorage,www,0,bytesToRead
WriteBytes tempstorage,save,0,bytesToRead ; download-to-file
FreeBank tempstorage
CloseFile save

CloseTCPStream www
Return 1
End Function
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

spicypixel

Quote from: Ocean on 2013-Nov-25
NETWEBGET$() or NETWEBGET(), depending on your needs, but no doubt you knew this already, as it is in the user manual?  Something perhaps not working as expected?

I was experimenting with NETWEBGET($) and had no problem with text files (.htm .php .ini .txt types etc.) but files which contained byte data I struggled to get any data (it seemed) into the string to display or work with?
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.