HTTPSocket

From Xojo Documentation
Jump to: navigation, search

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 HTTPStatusCode ReadOnlyProperty.png Port
BytesAvailable ReadOnlyProperty.png Handle ReadOnlyProperty.png RemoteAddress ReadOnlyProperty.png
BytesLeftToSend ReadOnlyProperty.png IsConnected ReadOnlyProperty.png RequestHeaders
ErrorCode LastErrorCode ReadOnlyProperty.png Yield
HTTPProxyAddress LocalAddress ReadOnlyProperty.png
HTTPProxyPort NetworkInterface


Methods
ClearRequestHeaders Listen SendRequest
Close Lookahead SetFormData
Connect PageHeaders SetPostContent
Disconnect Poll SetRequestContent
EncodeFormData Post SetRequestHeader
Get Purge
GetHeaders ReadAll


Notes

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, simply pass the optional parameter. For example,

Dim http As New HTTPSocket
MsgBox(http.Get("www.xojo.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.

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.

Examples

The following example retrieves the specified URL synchronously:

Dim socket1 As New HTTPSocket
Dim data As String = socket1.Get("http://www.xojo.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

HTTPSecureSocket, SocketCore, TCPSocket classes.

Personal tools
Namespaces

Variants
Actions
Main
Content
Starting Out
Dig Deeper
More Help
Toolbox
About