我一直在寻找有关此问题的文档,但似乎找不到我的问题的答案。我试图在我的perl脚本中使用Net :: HTTP将一些数据发布到url。问题是,我需要能够修改请求的标头(例如添加代理信息)。
有人有这样的简单例子吗?
您可以使用发出POST请求Net::HTTP
,但正如池上所指出的那样,大多数人都使用LWP::UserAgent
这种方法。
my $http = Net::HTTP->new('Host' => "stackoverflow.com")
or die "Unable to connect: $@";
$http->write_request('POST' => "/",
'User-Agent' => "Mozilla/4.0",
"foo=bar",
);
my ($code, $message, %h) = $http->read_response_headers;
if($code != 200) {
die "Request failed: $message";
}
my $response;
while(1) {
my $buf;
my $n = $http->read_entity_body($buf, 1024);
die "Read failed: $!" unless defined $n;
last unless $n;
$response .= $buf;
}
print "$response\n";
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句