繁体   English   中英

Perl:需要一个LWP和HTTP :: Request实际有效的POST代码

[英]Perl: Need an LWP & HTTP::Request POST code that actually works

我一直在努力尝试获取LWP和HTTP :: Request来将POST参数实际传递到Web服务器。 Web服务器可以看到该请求是POST事务的事实,但是它没有选择传递的参数。 我整天都在搜索此内容,并尝试了不同的方法,但尚未找到可行的方法。 (Web服务器正在工作,我能够手动发送发布交易,并且在运行整个脚本时,我的状态为“ 200”,但是看不到任何发布的元素。任何帮助将不胜感激。Tnx。

my $ua2 = LWP::UserAgent->new;
$ua2->agent("Mozilla/5.0 (compatible; MSIE 6.0; Windows 98)");
my $req2 = HTTP::Request->new(POST => "$url", [ frm-advSearch => 'frmadvSearch' ]);
$req2->content_type('text/html');
my $res2 = $ua2->request($req2);
$http_stat = substr($res2->status_line,0,3);
my $res = $ua->post($url,
   Content => [
      'frm-advSearch' => 'frmadvSearch',
   ],
);

这是短的

use HTTP::Request::Common qw( POST );

my $req = POST($url,
   Content => [
      'frm-advSearch' => 'frmadvSearch',
   ],
);

my $res = $ua->request($req);

这是一个Mojo :: UserAgent示例,我发现它更容易调试:

use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
$ua->transactor->name( 'Mozilla/5.0 (compatible; MSIE 6.0; Windows 98)' );

my $url = 'http://www.example.com/form/';
my $tx = $ua->post( $url, form => { 'frm-advSearch' => 'frmadvSearch' } );
say $tx->req->to_string;

$tx的事务知道请求,因此我可以看一下:

POST /form/ HTTP/1.1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (compatible; MSIE 6.0; Windows 98)
Accept-Encoding: gzip
Host: www.example.com
Content-Length: 26

frm-advSearch=frmadvSearch

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM