【问题标题】:LWP post returns empty files while curl works fineLWP 帖子返回空文件,而 curl 工作正常
【发布时间】:2013-11-10 15:03:06
【问题描述】:

我发现我的上传 perl 在我的 apache 上返回空文件,但使用 CURL 发布它可以正常工作:

@FILES = qw (o1.tmp o2.tmp o3.tmp); 

foreach $filename (@FILES) {

$ua=LWP::UserAgent->new;

$proxy = "http://$USERNAME:$PASSWORD\@$PROXY_ADD";


$ua->proxy(['http', 'https'], $proxy); 

my $ua = LWP::UserAgent->new;
$ua->timeout(10);
my $url = "http://www.XXXXXXXXX.es/handler.php";
my $picture = $filename;
my %args;
my $field_name = "userfile";
my $buf ;
my $buf_ref = $args{'buf'} || \$buf ;


my $value = read_file( $picture , binmode => ':raw' , scalar_ref => 1 );

my $response = $ua->post( $url,
        Content_Type => 'form-data',
        Content => [ $field_name => ["$picture"] ]
        );
print $response->content;
}

返回空文件:

root@dasdasd:/var/www/upload# ls -l
total 0
-rw-r--r-- 1 www-data www-data 0 oct 30 20:08 tmp_30-10-2013_0808pm_5554.tmp
-rw-r--r-- 1 www-data www-data 0 oct 30 20:08 tmp_30-10-2013_0808pm_8437.tmp
-rw-r--r-- 1 www-data www-data 0 oct 30 20:08 tmp_30-10-2013_0808pm_8629.tmp

但是有了这个 cURL 代码:

XXX-MacBook-Pro:Desktop xxxxxx$ curl -X PUT --header "Content-Type:multipart/form-data" --header "Name: 1" --data-binary "@2.gif" -v http://www.xxxxxxxxxx.es/handler.php

作品:

root@Arvaro:/var/www/upload# ls -l
total 4
-rw-r--r-- 1 www-data www-data 0 oct 30 20:08 tmp_30-10-2013_0808pm_5554.tmp
-rw-r--r-- 1 www-data www-data 0 oct 30 20:08 tmp_30-10-2013_0808pm_8437.tmp
-rw-r--r-- 1 www-data www-data 0 oct 30 20:08 tmp_30-10-2013_0808pm_8629.tmp
-rw-r--r-- 1 www-data www-data 6 oct 30 20:40 tmp_30-10-2013_0840pm_1978.tmp
                              ^^^
                           not empty

任何人都可以在这里放一些光吗?

谢谢

【问题讨论】:

  • 您将 PUT 与 curl 一起使用,但将 POST 与 LWP 一起使用。您使用 curl 证明名称标头,但不使用 LWP。
  • 你的代码LWP代码有很多死代码,包括$ua->proxy(['http', 'https'], $proxy);
  • 响应的 HTTP 状态 ling 是什么?
  • $ua->proxy(['http', 'https'], $proxy);用于代理上的基本身份验证。我认为是需要的。关于其余的......当然......大量的死代码。我不知道响应的 HTTP 状态。我在脚本的输出上没有看到任何响应。关于放置/发布...我可以尝试在 LWP 中使用 PUT ..但我不知道正确的方法。如果您提出任何建议,我可以尝试一个代码(顺便说一句)。
  • 如果需要->proxy,那你就有问题了。您没有将它用于 $ua 用于 post

标签: php perl curl lwp


【解决方案1】:

首先,您发布的是文件名称,但不是该文件的内容。接下来在您的 cURL 中,您将使用一些标头...

我认为你需要这样的东西:

$ua->default_header('Name' => "1");


my $response = $ua->post( $url,
        Content_Type => 'multipart/form-data',
        Content => [ $field_name => $value ]
        );
print $response->content;

【讨论】:

  • 确定问题就在这里.. 但我没有关注要在 $value 上添加什么,我的意思是在我的示例中它假设发布 o1.txt o2.txt 和 o3.txt.. . 因为 $picture 是来自 bucle 的 $filename。
  • @alvgarci 您不能只给出要上传的文件的名称,您必须实际阅读并发送它。
  • 当然。我已经明白了。我正在研究它。关键是..我必须做一个典型的do while循环读取每行的所有文件行并在每一行上做一个帖子..还是我必须将所有帖子内容转储到一个变量并发布它?..
猜你喜欢
  • 2021-01-07
  • 1970-01-01
  • 1970-01-01
  • 2013-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-07
  • 1970-01-01
相关资源
最近更新 更多