tony1234567893 has asked for the wisdom of the Perl Monks concerning the following question:

Program Requirements
General:
After logging into a web site and traversing a few links,
I need to download a file.
Issue:
Manually, when I get to the file/link and the file/link
is chosen, an 'open-save-cancel' window appears to
which I choose the 'save' button. This is my dilemma
I think. In my program, I get to this last step and
my code does not get the file that the URL points to.

What could I be doing wrong? I get the 'file found'
but no file is downloaded.

Code is as follows:
#!/usr/bin/perl -w use strict; use WWW::Mechanize; use HTTP::Cookies; #use LWP::Debug qw(+); my $url = "https://home.host.net/"; my $username = "myuser"; my $password = "mypassword"; my $mech = WWW::Mechanize->new(); # # Get URL and login # $mech->cookie_jar(HTTP::Cookies->new()); $mech->get($url); $mech->form_name('Form1'); $mech->field("txtUsername", $username); $mech->field("txtPassword", $password); $mech->click(); my $url2 = "https://home.myhost.net/test/Reports/Users/FileToDownload. +zip"; my $tempfile = "C:/MyLocalFile.zip"; my $contents = $mech->get($url2, ':content_file' => $tempfile ); print "link found\n" if defined $contents;

Cheers (most humbly)

Replies are listed 'Best First'.
Re: download a file from a web page
by Khen1950fx (Canon) on Aug 02, 2010 at 21:22 UTC
    Try adding noproxy => 1 to the constructor
    my $mech = WWW::Mechanize->new( noproxy => 1 );
      Thanks for your feedback. That helps but I still do not get the file. I instead get the following error stack,

      LWP::UserAgent::_need_proxy: Not proxied
      LWP::Protocol::http::request: ()
      LWP::Protocol::collect: read 816 bytes
      LWP::Protocol::collect: read 840 bytes
      LWP::UserAgent::request: Simple response: Unauthorized

      Cheers!