HTTPSocket

From Xojo Documentation

Class (inherits from TCPSocket)

Used to send and receive data via the HTTP 1.0 protocol.

Events
AuthenticationRequired Error ProxyAuthenticationRequired
Connected HeadersReceived ReceiveProgress
DownloadComplete PageReceived SendProgress
Properties
Address HTTPProxyPort NetworkInterface
BytesAvailable fa-lock-32.png HTTPStatusCode fa-lock-32.png Port
BytesLeftToSend fa-lock-32.png Handle fa-lock-32.png RemoteAddress fa-lock-32.png
ErrorCode IsConnected fa-lock-32.png RequestHeaders
HTTPProxyAddress LocalAddress fa-lock-32.png Yield
Methods
ClearRequestHeaders Listen SendRequest
Close Lookahead SetFormData
Connect PageHeaders SetPostContent
Disconnect Poll SetRequestContent
EncodeFormData Post SetRequestHeader
Get Purge
GetHeaders ReadAll

Notes

fa-info-circle-32.png
If you need HTTP 1.1 support, use URLConnection instead.

Use the HTTPSocket class to send and receive data via the HTTP protocol. Since this is a subclass of the TCPSocket class, all the standard socket APIs are also available. To perform secure communications (via HTTPS), use the HTTPSecureSocket class instead. It is otherwise identical, except that it is subclassed from SSLSocket instead of TCPSocket. This makes all the options for encrypted communications available to HTTPSecureSocket objects.

If you use a constructor in a subclass of HTTPSocket, you must call the super class's constructor in your subclass's constructor. The subclass fail when you try to download.

When using the optional Timeout parameter of the Get or GetHeaders methods the page will be received or posted in a synchronous manner. That is, your app will wait until the page has been received or posted before it proceeds. If Timeout is set to zero, the socket will wait indefinitely or until it receives an error.

To use synchronous mode, pass the optional parameter. For example,

Dim http As New HTTPSocket
MsgBox(http.Get("http://www.example.com", 30))

When using synchronous mode, you can set the Yield property to True to tell the socket to yield time to other events and code to execute while waiting. Simply insert a statement such as

http.Yield = True

to allow background processes to execute while waiting.

App Transport Security

Starting with OS X 10.11 and 2018r4, your apps have to use secure "https" connections or you will get this error: "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection". You can work around this by providing a plist with your app to indicate what non-secure URLs you are using. For more information:

Xojo Cloud

Web apps running on Xojo Cloud first have to use the FirewallPort class to open the port used to connect to an outside web server.

Sample Code

The following example retrieves the specified URL synchronously:

Dim socket1 As New HTTPSocket
Dim data As String = socket1.Get("http://www.example.com/", 30)

For asynchronously transfers you need to subclass HTTPSocket, call get without timeout parameter and get result in HTTPSocket.PageReceived event.

The following example posts a simple form:

Dim form As Dictionary
Dim socket1 As New HTTPSocket

// create and populate the form object
form = New Dictionary
form.Value("firstname") = "Bob"
form.Value("lastname") = "Brown"

// setup the socket to POST the form
socket1.SetFormData(form)
socket1.Post("http://www.myformlocation.com/form.php")

To send one or more cookies with an HTTP request:

socket.SetRequestHeader("Cookie", Join(Array(cookie1, cookie2), "; "))

See Also

URLConnection, HTTPSecureSocket, SocketCore, TCPSocket, Xojo.Net.HTTPSocket classes.