HttpClient
(gb.net.curl)
Symbols
Example
PUBLIC SUB getfile()
'How to download a file from the internet synchronously
'On 2007-jun-17, Daniel Compos noted that for Gambas v 1.9.49+:
' Put the "Async" PROPERTY set TO FALSE prior TO Get(), that way
' Get() will STOP the program flow UNTIL all the information IS
' received. In that CASE you should USE also the "TimeOut" PROPERTY TO
' set a timeout, IF NOT, it could hang forvever IF the server does NOT
' reply properly.
DIM h AS HttpClient
DIM buffer AS String
h = NEW HttpClient AS "h"
h.URL = "http://elinks.or.cz/"
h.Async = FALSE
h.Timeout = 60
h.Get
PRINT "begin"
IF h.Status<0 THEN
PRINT "ERROR"
ELSE
' Success - read the data
IF Lof(h) THEN READ #h, buffer, Lof(h)
PRINT buffer
END IF
PRINT "end"
END
This example shows how you can download a file from the internet asynchronously. Call the DownloadAsync method with your url. Then when the download is complete display the HTML in the Finished event.
PUBLIC _downloadAsync AS NEW HttpClient AS "_Download"
PRIVATE downloadBuffer AS String
PUBLIC SUB DownloadAsync(URL as String)
downloadBuffer = ""
_downloadAsync.URL = URL
_downloadAsync.TimeOut = 20
_downloadAsync.Get()
END
PUBLIC SUB _Download_Connect()
PRINT "Connection found " & _downloadAsync.URL
END
PUBLIC SUB _Download_Read()
DIM buffer AS String
READ #LAST, buffer, Lof(LAST)
downloadBuffer &= buffer
END
PUBLIC SUB _Download_Error()
PRINT "Error " & _downloadAsync.Status & " downloading " & _downloadAsync.URL
END
PUBLIC SUB _Download_Finished()
PRINT downloadBuffer
END