簡體   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