perl调用cgi的方法介绍

发布时间:2020-11-01编辑:脚本学堂
有时,需要在perl中非交互地调用已有的cgi来完成一定的功能,此时需要模拟一个http请求来调用cgi。

参考文章:
http://www.jb200.com/article/4403.html
http://www.willmaster.com/library/manage-forms/using_perl_to_submit_a_form.php

有时,需要在perl中非交互地调用已有的cgi来完成一定的功能,此时需要模拟一个http请求来调用cgi。
 
get方式调用:
 

复制代码 代码如下:
use HTTP::Request::Common;
use LWP::UserAgent;
$user_agent = LWP::UserAgent->new;
$request = GET 'http://clearcase/~xhzhu/cgi/cgireader.cgi?text1=hello&text2=here';
$response = $user_agent->request($request);
print $response->as_string;

post方式调用:
 

复制代码 代码如下:
use HTTP::Request::Common;
use LWP::UserAgent;
$user_agent = LWP::UserAgent->new;
$request = POST 'http://clearcase/~xhzhu/cgi/cgireader.cgi',
[text1 => 'Hello', text2 => 'there'];
$response = $user_agent->request($request);
print $response->as_string;

文件:cgireader.cgi
 

复制代码 代码如下:

#!/usr/local/bin/perl
use CGI;

$co = new CGI;

print $co->header,

$co->start_html(
  -title=>'CGI Example',
  -author=>'yourName',
  -BGCOLOR=>'white',
  -LINK=>'red'
);

if ($co->param()) {
    print
    "You entered this text: ",
    $co->em($co->param('text1')),
    " ",
    $co->em($co->param('text2')),
    ".";
 } else {
   print "Sorry, I did not see any text.";
}
print $co->end_html;

作者:iTech
出处:http://itech.cnblogs.com/