Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 我可以在UserAgent post方法中传递GET字符串吗_Perl - Fatal编程技术网

Perl 我可以在UserAgent post方法中传递GET字符串吗

Perl 我可以在UserAgent post方法中传递GET字符串吗,perl,Perl,我在这种模式下调用: 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&param2=val2' ); 这是因为我使用的是Fi

我在这种模式下调用:

 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&param2=val2' );
这是因为我使用的是Firebug,当我进入“POST”选项卡下的Net选项卡时,它会显示单个参数以及POST提交请求的GET字符串。 所以我想知道在这个函数调用中是否使用GET字符串

参数应用程序/x-www-form-urlencoded
Itemid 4选项com_搜索
searchword dsd任务搜索源
内容类型:
应用程序/x-www-form-urlencoded
内容长度:53
searchword=dsd&task=search&option=com\u search&Itemid=4


简而言之,您可以传递GET strings yes,但如果您的最终代码不接受GET方法,它将失败。

此外,您可能仍然需要指定一些参数,因为post方法要求

与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";
}

假设我在评论文本字段中输入了“你好”。所以firebug显示“hi+there”作为参数。在$req->content中,我应该使用这个值加“+”还是作为原始值?@AgA我不这样认为不,因为它是通过POST发送的,您可以在收到它的页面上打印出来以确保它。@AgA为了确保它,我运行了一个测试,如果您使用上述代码发送它,则不需要以任何方式对它进行编码。它可以邮寄出去。
# 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";
}