【问题标题】:How do you save a file from NTLM authenticated URL in Perl Script如何在 Perl 脚本中保存来自 NTLM 身份验证 URL 的文件
【发布时间】:2013-11-13 22:41:55
【问题描述】:

我真的是 Perl 脚本的新手,我正在开发一个脚本来访问具有 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-> 内容保存到一个文件夹中。就像我说的,这个 $res->content 是一个 .xls、.doc、.ppt 等类型的文件。在此先感谢

【问题讨论】:

  • 您应该显示一些代码以及您的问题所在。
  • 我已经更新了我的问题。谢谢

标签: perl download ntlm


【解决方案1】:

像这样:

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 ); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2018-03-06
    • 2019-10-02
    相关资源
    最近更新 更多