perl发送http请求,easy之极
perl 发送http 非常之简单,发送get请求 只一行代码use LWP::Simple;
$content = get('http://localhost:8080/webtest/a.jsp');
发送post,或者要发送请求头,cookie,则为以下代码,也不多
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new('POST' => 'http://localhost:8080/webtest/a.jsp');
$req->content_type('application/x-www-form-urlencoded');#post请求,如果有发送参数,必须要有这句
$req->header('Cookie' => "key1=value1;key2=value2"); #如果想发送cookie,则需这句
$req->header('Accept-Language' => 'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3');#如需发送请求头,
#只需像这行代码一般
$req->content("name=zhangsan&id=123");#发送post的参数
my $res = $ua->request($req);
print $res->status_line."\n";
print $res->as_string();#获取的是原始内容,包括响应头,响应正文
#$res->content();获取的是响应正文
页:
[1]