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

I'm having problems downloading binary files using HTTP::Response. For example, in the case of the code below, the original file is 5344 bytes, but the downloaded version is 5377 bytes. Here's the code:
#!/usr/bin/perl -w use strict; use LWP::UserAgent; use HTTP::Request; use HTTP::Response; use IO::File; my $url = 'http://www.aoov08.dsl.pipex.com/test.wma'; $url = URI->new($url); my $ua = LWP::UserAgent->new(); my $req = HTTP::Request->new(GET => $url); my $response = $ua->request($req); if ($response->is_error()) { print "Error " , $response->status_line, "\n"; } else { open(FILE, "> test.wma"); print FILE $response->content(); close(FILE); } exit;
Could anyone cast any light on what I'm doing wrong, please?

Replies are listed 'Best First'.
Re: HTTP::Response problem
by Corion (Patriarch) on Feb 23, 2004 at 09:01 UTC

    When storing binary data, it's always prudent to tell Perl that you intend to do so:

    my $filename = "test.wma"; open FILE, ">", $filename or die "Couldn't create '$filename' : $!"; binmode FILE; print FILE $response->content; close FILE;

    You might be interested in using LWP::Simple instead if no other interaction is required :

    use strict; use LWP::Simple; my ($url,$filename) = ('http://www.aoov08.dsl.pipex.com/test.wma', 'te +st.wma'); my $result = getstore($url,$filename);