LWP GET大文件下载

时间:2013-10-31 00:14:47

标签: perl lwp

我有一个特殊的文件下载案例。我需要为大文件进行分块下载,并且还需要在下载之前将参数传递给CGI脚本。

它实际上是一个REST接口。我在互联网上搜索过,下载部分有很多东西,参数部分有很多内容,但是当我把它们放在一起时,我会遇到错误。另请注意,我以类似的方式进行POST,并且工作正常。这是我的代码片段:

# $filename, $target, $url, $bs, etc. are all set...
my $bytes_received = 0;

open (FH, ">", "$filename") or $logger->error("Couldn't open $filename for writing: $!" );
my $ua = LWP::UserAgent->new();
my $res = $ua->get(
    $url,
    ':content_cb' => \&callback,
    'Content' => {
    "api" => 'olfs',
    "cmd" => 'rfile',
    "target" => $target,
    "bs" => $bs});

    print $logger->info("$bytes_received bytes received");

sub callback{
    my($chunk, $res) = @_;
    $bytes_received += length($chunk);
    print FH $chunk;
}

以下是错误:

Not a SCALAR reference at /usr/local/share/perl5/HTTP/Message.pm line 163.
 at /usr/local/share/perl5/HTTP/Message.pm line 163
    HTTP::Message::add_content('HTTP::Request=HASH(0x1956a88)', 'HASH(0x7fdfda565e88)') called at /usr/local/share/perl5/HTTP/Request/Common.pm line 111
    HTTP::Request::Common::_simple_req(undef, undef) called at /usr/local/share/perl5/HTTP/Request/Common.pm line 20
    HTTP::Request::Common::GET('http://10.0.0.15:8084/cgi-bin/olss.cgi', 'Content', 'HASH(0x7fdfda565e88)') called at /usr/local/share/perl5/LWP/UserAgent.pm line 410
    LWP::UserAgent::get('LWP::UserAgent=HASH(0x191a220)', 'http://10.0.0.15:8084/cgi-bin/olss.cgi', ':content_cb', 'CODE(0x1845818)', 'Content', 'HASH(0x7fdfda565e88)') called at ./olfs_get.pl line 72
Debugged program terminated.  Use q to quit or R to restart,
  use o inhibit_exit to avoid stopping after program termination,
  h q, h R or h o to get additional info.
  DB<3> print oct("764")
500
  DB<4>

1 个答案:

答案 0 :(得分:1)

  

$ ua&gt; get($ url)
  $ ua&gt; get($ url,$ field_name =&gt; $ value,...)

     

此方法将在给定的$ url上发送GET请求。可以给出进一步的参数来初始化请求的头部。

没有Content标题。 ->post使用它来创建消息体,它从不用于GET请求。如果您要构建网址,可以使用URI

  

$ ua-&gt; post($ url,$ field_name =&gt; $ value,... Content =&gt; \%form)
  $ ua&gt; post($ url,$ field_name =&gt; $ value,... Content =&gt; \ @form)
  $ ua&gt; post($ url,$ field_name =&gt; $ value,... Content =&gt; $ content)