URL to Internet

Discussions about the Liberty BASIC language, with particular reference to LB Booster
User avatar
JackKelly
Posts: 63
Joined: Fri Apr 06, 2018 2:38 am
Location: Rome NY, USA

Re: URL to Internet

Post by JackKelly »

It did indeed work quite well. Thank you. I never would have thought to look in Rosetta for the sample code. Here's a working demo, to save others a bit of head scratching.

Code: Select all

URL$="http://tgftp.nws.noaa.gov/data/observations/metar/stations/KPHL.TXT"
FileName$="ReceivedData$"
ReturnCode=DownloadToFile(URL$, FileName$)
if  ReturnCode=0 then
    open FileName$ for input as #f
        FileText$ = input$(#f, LOF(#f))
    close #f
    print FileText$
else
    print "ReturnCode = "; ReturnCode
end if
print "Program Complete."
end
 
function DownloadToFile(urlfile$, localfile$)
    open "URLmon" for dll as #url
        calldll #url, "URLDownloadToFileA",_
            0 as long,_             'null
            urlfile$ as ptr,_       'url to download
            localfile$ as ptr,_     'save file name
            0 as long,_             'reserved, must be 0
            0 as long,_             'callback address, can be 0
            DownloadToFile as ulong '0=success
    close #url
end function
guest
Site Admin
Posts: 197
Joined: Tue Apr 03, 2018 1:34 pm

Re: URL to Internet

Post by guest »

JackKelly wrote: Thu May 28, 2020 12:44 pm

Code: Select all

URL$="http://tgftp.nws.noaa.gov/data/observations/metar/stations/KPHL.TXT"
That should be ftp:// rather than http:// surely? If it's also available via the HTTP protocol you should be able to use httpget$() which is a built-in function in Liberty BASIC 4.5 and is available in LBB via the lb45func.bas library.

And indeed I can confirm that this does work. Seems to me that you have been making a mountain out of a molehill (and the information you received that it was only available via FTP was at the very least misleading):

Code: Select all

print httpget$("http://tgftp.nws.noaa.gov/data/observations/metar/stations/KPHL.TXT")
end
'include lb45func.bas
User avatar
JackKelly
Posts: 63
Joined: Fri Apr 06, 2018 2:38 am
Location: Rome NY, USA

Re: URL to Internet

Post by JackKelly »

Hmm. Both programs work regardless if the message is prefixed with http:// or ftp://. I guess I misunderstood that that the data was available only by FTP. Is this all a molehill? Over and out.
guest
Site Admin
Posts: 197
Joined: Tue Apr 03, 2018 1:34 pm

Re: URL to Internet

Post by guest »

JackKelly wrote: Fri May 29, 2020 1:10 am Both programs work regardless if the message is prefixed with http:// or ftp://.
That's probably because the file is available via HTTP anyway. My understanding is that URLDownloadToFile will successfully download a file using FTP (so long as the URL starts ftp:// and there is no requirement for a username or password) but I would expect httpget$() to work only if it can be downloaded via HTTP (probably the specified protocol is ignored in that case). Obviously when httpget$() works it's far easier than any other solution, especially in LB 4.5.