Tips   >   Internet   >   Internet

Internet

This section covers topics releated to using the internet from Omnis Studio. (Email, FTP, HTTP, TCP, etc.)

HTTP Download File

You can use the HTTPPage command to download a file from a web server. The trick is to remove the header from the file before saving it to the local computer.

The following sample code downloads a zip file and prompts the user for a file name and location to save the file.

; Ping the server to make sure we are connected to the internet.
Calculate Server as 'studiotips.net'
TCPPing (Server) Returns Milliseconds
If Milliseconds<0
   OK message (Icon) {Unable to ping the server at '[Server]'}
   Calculate FlagOK as kFalse
Else
   
   ; Figure out the URL to the file to be downloaded.
   ; The file could be any file type: zip, pdf, jpg, txt, etc.
   Calculate DownloadsDirURL as 'http://www.studiotips.net/downloads/free/'
   Calculate FileName as 'test.zip'
   Calculate URL as con(DownloadsDirURL,FileName)
   
   ; Load the file into a character variable.
   HTTPPage (URL) Returns Buffer
   If len(Buffer)=0
      OK message (Icon) {The server returned an empty page for the URL:////[URL]}
      Calculate FlagOK as kFalse
   Else
      
      ; Remove the HTTP header and copy the character variable contents to a binary variable.
      ; The header is separate from the content by an empty line.
      Calculate Find as con(kCr,kLf,kCr,kLf)
      Calculate Binary as mid(Buffer,pos(Find,Buffer)+len(Find))
      
      ; Prompt the user for a file name and location to save the file.
      Calculate Path as FileName
      ; (path[,prompt,filter,initial-directory,appflags])
      Do FileOps.$putfilename(Path,'Save Downlioaded File As') Returns bContinue
      If not(bContinue)
         Calculate FlagOK as kTrue
      Else
         
         ; Create the file using the FileOps external.
         Do oFileOpsExt.$createfile(Path) Returns FlagOK
         If FlagOK
            Do oFileOpsExt.$writefile(Binary) Returns FlagOK
         End If
         Do oFileOpsExt.$closefile()
         If not(FlagOK)
            OK message (Icon) {FileOps error when attempting to create and write to the file at:////[Path]}
         Else
            OK message (Icon) {The downloaded file has been saved at:////[Path]}
         End If
      End If
   End If
End If
Quit method FlagOK

HTTPPage

The Omnis command HTTPPage returns the contents of a web page.

; Ping the server to make sure we are connected to the internet.
Calculate Server as 'studiotips.net'
TCPPing (Server) Returns Milliseconds
If Milliseconds<0
   OK message (Icon) {Unable to ping the server at '[Server]'}
Else
   
   ; Get the home page contents.
   Calculate URL as 'http://www.studiotips.net'
   HTTPPage (URL) Returns Text
   
   OK message (Icon) {StudioTips home page content is:////[Text]}
   Breakpoint {Right-click on the Text variable to see the full contents.}
   
End If
Quit method Text

Click the Run Demo button in the StudioTips Browser to test the sample code.

Open Web Browser to URL

This tip provides cross platform code for opening the default web browser and pointing it to a specified URL. The Go to URL button in the StudioTips Browser window uses this code.

You can copy the sample code included with this tip to your library.

; Open the default browser to a specified URL.
Switch sys(6)
      
   Case 'M','X' ;; Mac, Mac OS X
      
      Calculate ScriptText as con('open location "',iMyURL,'"')
      Calculate ScriptText as con('$runapplescript(',chr(39),ScriptText,chr(39),')')
      Do [ScriptText]
      
   Case 'W','N' ;; Windows, Windows NT or 2000
      
      Register DLL ('Shell32.dll','ShellExecuteA','JJCCCCJ') Returns ErrorCode
      Call DLL ('Shell32.dll','ShellExecuteA',0,'#NULL#',iMyURL,'','#NULL#',1)
      
   Default
      
      OK message [sys(85)] (Icon) {Sorry I don't know how to open your Internet browser}
      
End Switch