I am tasked with daily downloads of transaction files from within a windows 2000 environment. In the past, I have used the URLDownloadToFile successfully with the secure websites, but doing so within an ugly VBA macro. I want desperately to convert all possible from VBA to perl scripts.
Here's the problem. If the link to the download file is on a page requiring authentication, I'm getting a couple of types of non-zero return codes, all of which seems to point to authentication errors (Access denied) -- it appears the authentication from the open IE browser session isn't being considered when the URLDownloadToFile function is called. If the file to download is NOT on an authenticated page, the code works fine.
What am I missing - how can I tie in the authentication when calling URLDownloadToFile from within the perl script? There seems to be a piece of the puzzle I'm not considering -- so, thanks in advance for any clue as to what I am missing.
Example very simplified code below (w/o actual links and login info), using IEAutomation module to interact with IE. I have also tried with IE::Mechanize, with the same results. I am open to other suggestions, however.
#!/usr/bin/perl
use Win32::API;
use Win32::IEAutomation;
my $ie = Win32::IEAutomation->new( visible => 1, maximize => 1);
my $GetUrl = new Win32::API('urlmon', 'URLDownloadToFile', 'NPPNN', 'N');
if(not defined $GetUrl) {
die "Can't import API urlmon: $!\n";
}
$ie->gotoURL('
https://mysecure.download.site')
;
$ie->getTextBox('name:', "user")->SetValue("User");
$ie->getTextBox('name:', "password")->SetValue("Pas
sword");
$ie->getButton('name:', "LogIn")->Click;
$recode = $GetUrl->Call(0, "
https://mysecure.download.site/FileIWant.txt"
, "C:\\SomeFile.txt", 0, 0);
print "Return Code: $recode\n";