2011-09-06 73 views
1

有誰知道爲什麼下面的代碼不會從請求的正文中的WWW :: Curl :: Form對象發送POST數據?WWW :: Curl無法在POST正文中附加WWW :: Curl :: Form數據

#!/usr/bin/perl 
    use strict; 
    use warnings; 
    use WWW::Curl::Easy; 
    use WWW::Curl::Form; 

    my $curl = new WWW::Curl::Easy(); 
    $curl->setopt(CURLOPT_VERBOSE, 1); 
    $curl->setopt(CURLOPT_NOSIGNAL, 1); 
    $curl->setopt(CURLOPT_HEADER, 1); 
    $curl->setopt(CURLOPT_TIMEOUT, 10); 
    $curl->setopt(CURLOPT_URL, 'http://localhost/post_test.php'); 

    my $curlf = new WWW::Curl::Form(); 
    $curlf->formadd('a','b'); 
    $curlf->formadd('c','d'); 
    $curlf->formadd('e','f'); 
    $curlf->formadd('g','h'); 
    $curlf->formadd('i','j'); 
    $curl->setopt(CURLOPT_HTTPPOST, $curlf); 

    my $resp = ''; 
    open(my $resp_fh, ">", \$resp); 
    $curl->setopt(CURLOPT_WRITEDATA, $resp_fh); 

    my $retcode = $curl->perform(); 
    die($retcode) if ($retcode != 0); 

    print $resp; 

這是POST請求,我看到(無論是在冗長的輸出,並通過Wireshark的):

POST /post_test.php HTTP/1.1 
    Host: localhost 
    Accept: */* 
    Content-Length: 0 

正如你可以看到有沒有內容類型,內容長度爲0,有身體沒有數據。

這是在Debian上使用libcurl3 7.21.0-2和libwww-curl-perl 4.12-1。

+0

的postit2.c例如工作正常,提交的multipart/form-data的數據。該庫看起來很好,它可能是WWW :: Curl包裝中的一個問題。 – Steve

回答

1

嘗試使用其它包裝,Net::Curl

#!/usr/bin/perl 
use strict; 
use warnings; 
use Net::Curl::Easy qw(:constants); 
use Net::Curl::Form qw(:constants); 

my $curl = new Net::Curl::Easy(); 
$curl->setopt(CURLOPT_VERBOSE, 1); 
$curl->setopt(CURLOPT_NOSIGNAL, 1); 
$curl->setopt(CURLOPT_HEADER, 1); 
$curl->setopt(CURLOPT_TIMEOUT, 10); 
$curl->setopt(CURLOPT_URL, 'http://localhost/post_test.php'); 

my $curlf = new Net::Curl::Form(); 
$curlf->add(CURLFORM_COPYNAME ,=> 'a', CURLFORM_COPYCONTENTS ,=> 'b'); 
$curlf->add(CURLFORM_COPYNAME ,=> 'c', CURLFORM_COPYCONTENTS ,=> 'd'); 
$curlf->add(CURLFORM_COPYNAME ,=> 'e', CURLFORM_COPYCONTENTS ,=> 'f'); 
$curlf->add(CURLFORM_COPYNAME ,=> 'g', CURLFORM_COPYCONTENTS ,=> 'h'); 
$curlf->add(CURLFORM_COPYNAME ,=> 'i', CURLFORM_COPYCONTENTS ,=> 'j'); 
$curl->setopt(CURLOPT_HTTPPOST, $curlf); 

my $resp = ''; 
open(my $resp_fh, ">", \$resp); 
$curl->setopt(CURLOPT_WRITEDATA, $resp_fh); 

my $retcode = $curl->perform(); 
die($retcode) if ($retcode != 0); 

print $resp;