Gambas Documentation
Application Repository
Code Snippets
Compilation & Installation
Components
gb
gb.args
gb.cairo
gb.chart
gb.clipper
gb.complex
gb.compress
gb.crypt
gb.data
gb.db
gb.db.form
gb.db.mysql
gb.db.odbc
gb.db.postgresql
gb.db.sqlite2
gb.db.sqlite3
gb.dbus
gb.dbus.trayicon
gb.debug
gb.desktop
gb.desktop.gnome.keyring
gb.desktop.x11
gb.eval
gb.eval.highlight
gb.form
gb.form.dialog
gb.form.editor
gb.form.htmlview
gb.form.mdi
gb.form.print
gb.form.stock
gb.form.terminal
gb.gmp
gb.gsl
gb.gtk
gb.gtk.opengl
gb.gtk3
gb.gtk3.opengl
gb.gtk3.webview
gb.gui
gb.gui.opengl
gb.gui.qt
gb.gui.qt.ext
gb.gui.qt.opengl
gb.gui.qt.webkit
gb.gui.trayicon
gb.gui.webview
gb.hash
gb.highlight
gb.image
gb.image.effect
gb.image.imlib
gb.image.io
gb.inotify
gb.jit
gb.libxml
gb.logging
gb.map
gb.markdown
gb.media
gb.media.form
gb.memcached
gb.mime
gb.mysql
gb.ncurses
gb.net
gb.net.curl
.Curl.Proxy
.Curl.SSL
Curl
DictClient
FtpClient
HttpClient
_new
Auth
Code
CookiesFile
CopyFrom
Download
Encoding
Get
Head
Headers
Post
PostFile
Put
PutFile
Reason
Redirect
Stop
TargetFile
UpdateCookies
UserAgent
HttpForm
Net
gb.net.pop3
gb.net.smtp
gb.openal
gb.opengl
gb.opengl.glsl
gb.opengl.glu
gb.opengl.sge
gb.openssl
gb.option
gb.pcre
gb.pdf
gb.poppler
gb.qt4
gb.qt4.ext
gb.qt4.opengl
gb.qt4.webkit
gb.qt4.webview
gb.qt5
gb.qt5.ext
gb.qt5.opengl
gb.qt5.webkit
gb.qt5.webview
gb.qt6
gb.qt6.ext
gb.qt6.opengl
gb.qt6.webview
gb.report
gb.report2
gb.scanner
gb.sdl
gb.sdl.sound
gb.sdl2
gb.sdl2.audio
gb.settings
gb.signal
gb.term
gb.test
gb.util
gb.util.web
gb.v4l
gb.vb
gb.web
gb.web.feed
gb.web.form
gb.web.gui
gb.xml
gb.xml.html
gb.xml.rpc
gb.xml.xslt
Controls pictures
Deprecated components
Developer Documentation
Development Environment Documentation
Documents
Error Messages
Gambas Playground
How To's
Language Index
Language Overviews
Last Changes
Lexicon
README
Search the wiki
To Do
Topics
Tutorials
Wiki License
Wiki Manual

HttpClient (gb.net.curl)

This class provides an HTTP client that sends requests to an HTTP server, and receives the response.

This class inherits Curl.

This class is creatable.

Inherited static properties
DefaultProxy   Return or set default proxy of all newly created HttpClient objects.

Static methods
Download  

Properties
Auth   Return or set the HTTP authentication mode to use.
Code   Returns the HTTP Status Code sent by the server.
CookiesFile   Return or set the file that will be used for reading and storing cookies.
Encoding   Return or set the "Accept-Encoding" HTTP header.
Headers   Return the HTTP headers as a String array.
Reason   Return the HTTP reason string.
Redirect   Return or set if the HttpClient must follow HTTP redirections.
UpdateCookies   Return or set if cookies must be updated. The default value is False.
UserAgent   Returns or sets the UserAgent used to make the request.

Inherited properties
Async   Returns or sets whether the FTP/HTTP request will be asynchronous.
Blocking   Returns or sets if the stream is blocking.
BufferSize   Return or set the preferred receive buffer size, between 0 and 512 KB.
ByteOrder   Returns or sets the byte order used to read or write binary data to the stream.
Debug   Return or set the debugging mode.
Downloaded   Return the number of bytes that have been downloaded.
EndOfFile   This property signals whether the last use of LINE INPUT reached the end of file, instead of reading a full line with an end-of-line character.
EndOfLine   Return or set the newline separator used by this stream.
Eof   Return if a stream reached its end.
ErrorText   Return the error string associated with the error code returned by the curl library.
Handle   Returns the system file descriptor associated with this Stream.
IsTerm   Return if a stream is associated with a terminal.
Lines   Returns a virtual object that allows you to enumerate a stream line by line.
NoShare  
NullTerminatedString   Return or set if strings are null-terminated when they are serialized.
Password   Returns or sets the password used for authorization.
Proxy   Returns a virtual object used for defining proxy parameters.
SSL   This virtual property allows to define the SSL properties of the CURL connection.
Status   Returns the status of the client.
Tag   Returns or sets the tag associated with the stream.
TargetFile   Return or set the target file used for download operations.
Term   Return a virtual object that allows to manage the terminal associated with the stream.
Timeout   Returns or sets the client timeout in seconds.
TotalDownloaded   Returns the total number of bytes that are expected to be downloaded.
TotalUploaded   Returns the total number of bytes that are expected to be uploaded.
URL   Return or set the URL of the resource to download.
Uploaded   Return the number of bytes that have been uploaded.
User   Returns or sets the user used for authorisation.

Methods
CopyFrom   Fill the HttpClient object from the properties of another HttpClient object.
Get   This method performs a call to the http server using the standard 'GET' method.
Head   This method performs a call to the http server using the standard 'HEAD' method.
Post   This method performs a call to the http server using the standard 'POST' method.
PostFile   Send a file through a POST request.
Put   Send some data to a specific URL by using the "PUT" method.
PutFile   Send a file through a PUT request.
Stop   Aborts the request.

Inherited methods
Begin   Start to buffer data written to the stream, so that everything will be sent when the Send method is called.
Close   Closes the stream.
Drop   Drop the data that has been buffered since the last call to the Begin method.
Peek   Returns the contents of the internal stream buffer.
ReadLine   Read a line of text from the stream, like the LINE INPUT instruction.
Send   Send all the data, in one shot, since the last call to Begin.
Watch   Start or stop watching the stream file descriptor for reading or writing, after it has been opened.

Inherited events
Cancel   This event is raised when a request has been cancelled.
Connect   Raised when the connection is established.
Error   Raised when there was an error returned by the CURL library.
Finished   Raised when a command terminates correctly.
Progress   This event is raised periodically when something is downloaded or uploaded.
Read   Raised when there is some data received.

Example

' How to download a file from the internet synchronously

' 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 forever if the server does not
' reply properly.

Public Sub GetFile()

  Dim hClient As HttpClient
  Dim sBuffer As String

  hClient = New HttpClient AS "hClient"
  hClient.URL = "http://elinks.or.cz/"
  hClient.Async = FALSE
  hClient.Timeout = 60
  hClient.Get

  Print "Begin"
  If hClient.Status < 0 Then
    Print "ERROR"
  Else
    ' Success - read the data
    If Lof(hClient) Then sBuffer = Read #hClient, Lof(hClient)
    Print sBuffer
  End If

  Print "end"

End

Example #2

This second example shows how you can download a file from the internet asynchronously.

It calls the DownloadAsync method with your URL. Then, when the download is completed, it displays the received HTML in the Finished event handler.

' How to download a file from the internet asynchronously

Public hAsyncClient As New HttpClient AS "hAsyncClient"
Private sDownloadBuffer As String

Public Sub DownloadAsync(URL As String)

  sDownloadBuffer = ""
  hAsyncClient.URL = URL
  hAsyncClient.TimeOut = 20
  hAsyncClient.Async = TRUE
  hAsyncClient.Get()

End

Public Sub hAsyncClient_Connect()

  Print "Connected to " & hAsyncClient.URL

End

Public Sub hAsyncClient_Read()

  Dim sBuffer As String

  sBuffer = Read #Last, Lof(Last)
  sDownloadBuffer &= sBuffer

End

Public Sub hAsyncClient_Error()

  Print "Error " & hAsyncClient.Status & " while downloading " & hAsyncClient.URL

End

Public Sub hAsyncClient_Finished()

  Print sDownloadBuffer

End