2011-05-04 77 views
0

我在此模式下拨打:我可以通过GET字符串中的UserAgent的POST方法

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字符串。

Parametersapplication/X WWW的形式,进行了urlencoded
ITEMID 4选项com_search
搜索内容DSD任务搜索来源
的Content-Type:
应用程序/ x-WWW窗体-urlencoded
的Content-Length :53
搜索内容= DSD &任务=搜索&选项= com_search &的itemid = 4

回答

2

总之,您可以传递GET字符串yes,但是如果您的最终代码不接受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"; 
} 

Read more...

+0

假设我在注释文本字段中输入“你好”。所以firebug会显示“hi + there”作为参数。在$ req->内容中,我应该使用这个值与“+”或作为原始值? – AgA 2011-05-04 05:10:44

+0

@AgA我不这么认为,因为它是在POST中发送的,所以您可以在您收到的页面上打印出来以确保它。 – Prix 2011-05-04 05:13:13

+0

@AgA只是为了确保它,我运行了一个测试,如果你使用上面的代码发送它,它不需要以任何方式进行编码。它会发送邮件就好了。 – Prix 2011-05-04 05:18:55