How to use tsNet to check the status of a URL without downloading its contents using the HTTP HEAD request method
For some applications, it can be helpful to check that a URL is accessible without having to download its complete contents. This is particularly useful when preparing to stream a large audio or video file across the Internet.
The following example stack shows how you can use the tsNetHead command to check the validity of a URL.
Create a new stack then drag a button and a field onto it.
Set the name of the field to "Output".
Save this stack.
Edit the script of the button that you placed on the stack by right clicking on the button and selecting "edit script".
Add the following code.
on mouseUp pButtonNumber put empty into field "Output" -- The following line is the call to tsNetHeadSync put tsNetHeadSync("https://livecode.com/", tHeaders, tResult, tBytes) into tRecvHeaders if tResult begins with "tsneterr:" then answer tResult else put "HTTP response code is:" && tResult & cr & cr into field "Output" put "HTTP returned headers are:" && cr & tRecvHeaders after field "Output" end if end mouseUp
Note that the function returns the HTTP headers sent by the server when using the tsNetHead* functions with the HTTP protocol.
In comparison, the tsNetGet* functions return the contents of the URL that have been requested. The HTTP headers are still accessible via the rOutHeaders parameter to the function. See the LC dictionary for more information.