我打电话给这种模式:
my $ua = new LWP::UserAgent;
my $response= $ua->post('www.example.com', {param1=>'val1',param2=>'val2'...} );
我是否可以通过GET格式传递值来调用上述内容?:
my $response= $ua->post('www.example.com?param=val1¶m2=val2' );
这是因为我正在使用Firebug,当我进入“POST”选项卡下的Net选项卡时,它会显示各个参数以及POST提交请求的GET字符串。 所以我想知道我是否在这个函数调用中使用GET字符串。
Parametersapplication / X WWW的窗体-urlencoded
Itemid 4选项com_search
searchword dsd任务搜索来源
内容类型:
应用程序/ x-WWW窗体-urlencoded
内容长度:53
的搜索内容= DSD&安培;任务=搜索和安培;选项= com_search&安培; ITEMID = 4
答案 0 :(得分:2)
简而言之,您可以传递GET字符串是,但如果您的结束代码不接受GET METHOD,则会失败。
此外,您可能仍需要指定一些参数,因为post方法要求post(url,array_with_parameters)
。
sub post {
require HTTP::Request::Common;
my($self, @parameters) = @_;
my @suff = $self->_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1));
return $self->request( HTTP::Request::Common::POST( @parameters ), @suff );
}
与HTTP::Request
一起使用,您可以按照自己喜欢的方式在内容中指定:
# Create a user agent object
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1 ");
# Create a request
my $req = HTTP::Request->new(POST => 'http://www.example.com');
$req->content_type('application/x-www-form-urlencoded');
$req->content('searchword=dsd&task=search&option=com_search&Itemid=4');
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success) {
print $res->content;
} else {
print $res->status_line, "\n";
}