2011-05-04 82 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