URLConnection.Send

From Xojo Documentation

Revision as of 13:37, 31 August 2021 by PLefebvre (talk | contribs) (Clarified that the FolderItem parameter is for downloading to a file.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Method

URLConnection.Send(method As String, URL As String, timeout As Integer = 60)

Supported for all project types and targets.

Asynchronously sends a request using an HTTP method such as GET or POST. Results appear in the ContentReceived event. Send can be used to send both standard "http" and secure "https" requests.


Method

URLConnection.Send(method As String, URL As String, file As FolderItem, timeout As Integer = 60)

Supported for all project types and targets.

Asynchronously sends a request using an HTTP method such as GET or POST and returns (downloads) the output in file. The downloaded file is available in the FileReceived event. Send can be used to send both standard "http" and secure "https" requests.

Notes

The timeout is in seconds and currently can be a maximum of 60 seconds (75 on macOS). A connection that times out will raise the Error event.

You can view the timeout length on macOS with this Terminal command:

sysctl net.inet.tcp.keepinit

You can change it using this command (specify the value in milliseconds):

sysctl net.inet.tcp.keepinit=30000

Standard Method Definitions

GET and POST are most commonly used, but there are other methods as well. For example, to get just the headers you can use the HEAD method. The full list of methods is available on the W3 HTTP/1.1 Method Definitions page.

Sample Code

Sends a request. The ContentReceived event is called with the response:

MyConnection.Send("GET", "http://127.0.0.1:8080/GetData")

Sends a GET request, saving (downloading) the response to a file sent to the FileReceived event:

Var outputFile As FolderItem = SpecialFolder.Documents.Child("data.json")
MyConnection.Send("GET", "http://127.0.0.1:8080/GetData", outputFile)

To make a secure request, use "https" in the URL:

Var outputFile As FolderItem = SpecialFolder.Documents.Child("data.json")
MyConnection.Send("GET", "https://www.mydomain.com/GetData", outputFile)

See Also

UserGuide:App Transport Security topic