0

I'd like to download a file using HTTP. How do I do it?

Spikolynn
  • 4,067
  • 2
  • 37
  • 44

1 Answers1

1

I have investigated this further and have re-ordered my suggestions from last week as a result:

  1. The class 'CHttp' in VO's 'Internet' library has a method GetFile (the VO 2.5 "what's new" has a brief description on page 10). I've not tried it, though. You'll probably want something like this:

    local oSession as CHttp
    local lSuccess as logic
    
    oSession := CHttp{} 
    oSession:ConnectRemote("foo.example.com")      // Server domain name
    lSuccess := oSession:GetFile("bar/baz.pdf",;   // Remote filename
                                "c:\temp\baz.pdf",; // Local filename
                                lFailIfAlreadyExists)   
    oSession:CloseRemote()
    // If lSuccess, the file should be downloaded to cLocalFileName.
    // I don't know whether the filename arguments should use / or \ for directory separators.
    
  2. I think another way is to use the Windows ShellExecute function to invoke an external program which downloads the file. I found an example of ShellExecute here. I haven't tried this as I don't have a VO compiler (or help file!) available to me at the moment. I'm not sure whether this is a good way or not, and I don't know whether it's safe from people trying to run a malicious command by supplying a sneaky filename. But I think the following might work. It assumes you have the program curl.exe (see: curl) on your path, which is used for downloading the file. You may need the fully path of curl.exe instead. I'm not sure where the file will be saved by default (I think you can specify a working directory in the parameter labelled lpDirectory)

    local cParameters as string
    local cURL:="http://www.example.com/interesting.htm" as string
    local cLocalFile:="savefile.html" as string
    cParameters := "-o "+cLocalFile+" "+cURL
    
    ShellExecute(NULL /*Window handle*/,;
       String2PSZ("open"),;   
       String2PSZ("curl.exe"),;
       String2PSZ(cParameters),;
       NULL_PTR /* lpDirectory */,;
       SW_SHOWNORMAL) 
    

    See also the MSDN page for ShellExecute.

  3. There appears to be a method App:Run(cCommand) which can be used to start external applications

marnir
  • 1,187
  • 10
  • 13