FTP Download Using .NET


Microsoft's .NET documentation is exhaustive, but assumes comprehension of syntax for C# and/or VB coding which may not come naturally to the purist APL programmer.  This page shows a transliteration of one example into a function which can be used in a Dyalog (Version 12) environment, the original C# code is shown beside the APL.

     ∇(status contents)←{a}FtpDownload(url file user password);⎕IO;⎕ML;⎕USING;request;response;stream;reader
⍝ Get a file using FTP
 ⎕IO ⎕ML←0 3
 ⎕USING←'System,System.dll' 'System.Net' 'System.IO'           ⍝ using System; using System.IO; using System.Net; using System.Text;
 :Trap a/90
     request←FtpWebRequest.Create⊂'ftp://',url,'/',file        ⍝ FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
     request.Method←WebRequestMethods.Ftp.DownloadFile         ⍝ request.Method = WebRequestMethods.Ftp.DownloadFile;
     request.Credentials←⎕NEW NetworkCredential(user password) ⍝ request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
     response←request.GetResponse                              ⍝ FtpWebResponse response = (FtpWebResponse)request.GetResponse();
     stream←response.GetResponseStream                         ⍝ Stream responseStream=response.GetResponseStream();
     reader←StreamReader.New stream                            ⍝ StreamReader reader = new StreamReader(responseStream);
     contents←reader.ReadToEnd                                 ⍝ Console.WriteLine(reader.ReadToEnd());
     status←response.StatusDescription                         ⍝ Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
     reader.Close                                              ⍝ reader.Close();
     response.Close                                            ⍝ response.Close();
 :Else
     (status contents)←0 ⍬
 :EndTrap

    ∇

Sample usage:

  status contents←0 FtpDownload '88.97.16.226' 'APL/Readme.txt' 'anonymous' 'dick@dickbowman.org.uk'

Notes:
Page created 15 January 2009.
Copyright © Dogon Research 2009.