我是Perl Script的新手,我正在开发一个脚本来访问带有NTLM身份验证的URL,并将响应内容保存在一个文件夹中。此内容是.xls,.doc,.pdf,.ppt等文件。实际上,我能够开发NTLM身份验证代码。但我的另一个要求是将响应内容保存到服务器中的文件夹中。你能帮帮我吗?
#!/usr/bin/perl
use LWP::UserAgent;
use HTTP::Request::Common;
my $url = "http://myurl.com/AdsSDAF34141J";
my $ua = new LWP::UserAgent(keep_alive => 1);
my $username = 'ap\<username>';
my $password = '<password>';
$ua->credentials('myurl.com:80', '', $username, $password);
my $req = GET $url;
print "--Peforming request now...---------\n";
my $res = $ua->request($req);
print "--Done with request ...---------\n";
if ($res->is_success) {
print $res->content;
} else {
print "Error: " . $res->status_line . "\n";
}
exit 0;
我想将$ res-&gt;内容保存到文件夹中。就像我说的那样$ res-&gt;内容是.xls,.doc,.ppt等类型的文件。提前感谢
答案 0 :(得分:0)
像这样:
if ($res->is_success) {
my $filename = 'file.xls';
open(my $fh,'>',$filename) or die $!;
binmode($fh);
print $fh $res->content;
close($fh);
} else {
或
### this way it automatically stored in that file
my $filename = 'file.xls';
$ua->request( $req, $filename );