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

Hello!

I am trying to write a perl script using WWW:::Mechanize, to download a dictionary file from a server.

The file is a zip file. The program given below (error checks have been purposely removed),
This program runs without any errors/warnings,
and i can see the file downloaded in the specified location.

The download is supposed to get me a zip file,
but when i open the file using winzip/winrar i get an error.
When i download it as .zip, opening with winzip gives this error:"filename: start of central directory not found; Zip file corrupt. Possible cause:file transfer error."
When i download it as .tar, winzip gives this error:"Error reading header after processing 0 entries."
Winrar gives this error "<filename>: Unexpected end of archive".

The server is on a Free BSD machine, my program is on running on Windows(Active Perl 5.8.7)

I'm surely missing something here, and cannot find a way out of this problem! :(

I had sent this message to perl-beginners/ libwww lists earlier but no response.

Hence posting again :)
Please help!
Best regards,
Dhanashri
==========================

#!/usr/bin/perl -w use strict; use WWW::Mechanize; my $login = "admin"; my $password = "pass"; my $url = "http://abc.xyz.com"; my $mech = WWW::Mechanize->new(); $mech->get($url); #Log in print "\n\n\n\nLogging in.... \n\n\n\n"; $mech->submit_form( form_number => 1, fields => { user => $login, password => $password }, ); #Navigate to the download page $mech->follow ( 'Download' ); if ($mech->success) { #this page has only one form with the "download" button. #Download is for a zip file. $mech->click(); if ($mech->success) { my $filename = 'E:\Dhanashri\down.zip'; $mech->save_content ( $filename ); print "\nFile Downloaded!\n\n"; } else { print "\nDownload Failed!\n"; } } print "Logging out...\n"; $mech->follow ( 'logout' );
============================

Replies are listed 'Best First'.
Re: Using Mechanize to download a zip file?
by Corion (Patriarch) on Nov 07, 2005 at 11:53 UTC

    Have you looked at what content you actually get? Maybe it is not what you think. Maybe it is yet another HTML page or something. Maybe open up the downloaded file in a text editor to see what it contains.

      Maybe the module is broken :)? looks like a binmode is missing
      =head2 $mech->save_content( $filename ) Dumps the contents of C<< $mech->content >> into I<$filename>. I<$filename> will be overwritten. =cut sub save_content { my $self = shift; my $filename = shift; open( my $fh, ">", $filename ) or $self->die( "Unable to create $f +ilename: $!" ); print $fh $self->content; close $fh; }

      update: confirmed and reported

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

        The module might be still broken; when I attempt to use the $mech->save_content method in this code:

        use strict; use warnings; use LWP::UserAgent; use WWW::Mechanize; use Win32::FileOp; #use Win32::FileOp qw(ShellExecute); # ShellExecute("http://finance.yahoo.com"); my $mech = WWW::Mechanize->new(); $mech->get("http://finance.yahoo.com"); my $filename = 'c:yahoo.html'; $mech->save_content ( $filename );


        I get the following message:
        Can't locate object method "save_content" via package "WWW::Mechanize" at Yahoo. pl line 13, <DATA> line 164.
Re: Using Mechanize to download a zip file?
by polettix (Vicar) on Nov 07, 2005 at 11:56 UTC
    What does the file actually contain? Some servers could send different stuff based on the user-agent that queries for a resource, so you could be getting a simple page stating that your browser is not enabled to download stuff. Or you could possibly get some other type of error.

    So, try to open it with a (possibly hexadecimal) editor and see if it is some error page like this.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Using Mechanize to download a zip file?
by spatterson (Pilgrim) on Nov 07, 2005 at 12:22 UTC
    It might be worth adding print $mech->ct after calling $mech->success so you can see the indicated content type from the server.
    just another cpan module author