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