加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – 如何使用WWW进行分块传输编码上传:Mechanize?

发布时间:2020-12-16 06:06:53 所属栏目:大数据 来源:网络整理
导读:我正在尝试使用特定的Web服务,我可以使用以下命令成功执行上载: curl -X POST --header "Transfer-Encoding: chunked" -d @Downloads/file.pdf https://some.webservice/upload 我得到了一个json回复表明成功. 但是,我无法弄清楚如何对WWW :: Mechanize做同
我正在尝试使用特定的Web服务,我可以使用以下命令成功执行上载:

curl -X POST  --header "Transfer-Encoding: chunked" -d @Downloads/file.pdf https://some.webservice/upload

我得到了一个json回复表明成功.

但是,我无法弄清楚如何对WWW :: Mechanize做同样的事情.

$mech->post("https://" . $server . "/upload",Content_Type => 'multipart/form-data',Content => [upID => $upid,name => $dlfile,userID => 0,userK => 0,file_0 => [$dlfile]]);

这会收到一个类似的json响应,其中包含一个很大的错误消息.

我是否需要先显式设置Transfer-Encoding标头?还有其他一些技巧吗?谷歌对此没有太多了解,Perlmonks也没有,而且文档有点迟钝.

解决方法

您可以使用HTTP :: Request :: StreamingUpload来完成

my $starttime = time();
my $req = HTTP::Request::StreamingUpload->new(
    POST     => $url,path    => $file,headers => HTTP::Headers->new(
        'Transfer-Encoding' => 'chunked' 
    ),);

my $gen = $req->content;
die unless ref($gen) eq "CODE";

my $total = 0;
$req->content(sub {
    my $chunk = &$gen();
    $total += length($chunk);

    print "r$total / $size bytes ("
        . int($total/$size*100)
        . "%) sent,"
        . int($total/1000/(time()-$starttime+1))
        . " k / sec ";

    return $chunk;
});

my $resp = $ua->request($req);
print "n";

unless ($resp->is_success) {
    die "Failed uploading the file: ",$resp->status_line;
}

my $con = $resp->content;
return $con;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读