0

我有一个文件下载的特殊情况。我需要对大文件进行分块下载,还需要在下载之前将参数传递给 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>
4

1 回答 1

1

$ua->get( $url )
$ua->get( $url , $field_name => $value, ... )

此方法将在给定的 $url 上发送一个 GET 请求。可以提供更多参数来初始化请求的标头。

没有Content标题之类的东西。->post使用它来创建消息体,它从不用于 GET 请求。如果你想建立一个 url,你可以使用URI

$ua->post( $url, $field_name => $value,... 内容 => \%form )
$ua->post( $url, $field_name => $value,... 内容 => \@形式 )
$ua->post( $url, $field_name => $value,... 内容 => $content )

于 2013-10-31T00:26:03.907 回答