问题
I am trying to write a script in Perl that generates URL list from web page, than goes over URL list and downloads the linked files.
the problem is that the generated URL's links look something like
www.example.com/download.php?file=Li9OaW50ZW5kby9ORVMvVVNBLzEwIFlhcmQgRmlnaHQuemlw
when i place this link in browser it knows the true file name (i.e. cards.zip), i don't know how to get it using Perl.
how can i get using Perl the link's 'true' file name ?
回答1:
Inspect the headers, specifically Content-Disposition
.
回答2:
Amadan had it right, just showing the code i used to solve it:
use LWP::UserAgent qw( );
use LWP::Simple;
$url = 'http://www.example.com/download.php?file=Li9OaW50ZW5kby9ORVMvVVNBLzNEIEJhdHRsZXMgb2YgV29ybGQgUnVubmVyLnppcA==';
#get file name
my $ua = LWP::UserAgent->new();
$cnt = %{%{$ua->head( $url )}->{'_headers'}}->{'content-disposition'};
$cnt =~ m/filename=(.*)/;
print "File name is: $1\n";
#Save File
my $status = getstore($url, $1);
if ( is_success($status) ){
print "File $1 Saved Correctly !\n";
}else{
print "Error saving $1!\n";
}
来源:https://stackoverflow.com/questions/8782813/perl-resolving-link-download-name