LiveCode LessonsHow To - Step-By-Step Guides To Tasks In LiveCode TSNetHow to use tsNet to check the status of a URL without downloading its contents using the HTTP HEAD request method

How to use tsNet to check the status of a URL without downloading its contents using the HTTP HEAD request method

Introduction

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.

Lay out the Stack

Create a new stack then drag a button and a field onto it.

Set the name of the field to "Output".

Save this stack.

Create the Script

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

More information

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.

Test

Switch to Run mode and click the button.

0 Comments

Add your comment

E-Mail me when someone replies to this comment