我一直在查阅这方面的文档,但我似乎找不到问题的答案。我试图在perl脚本中使用Net::HTTP将一些数据发布到url中。问题是,我需要能够修改请求的头(比如添加代理信息)。
有人有这样的简单例子吗?
发布于 2014-06-19 17:08:50
您可以使用Net::HTTP发出POST请求,但是正如ikegami所指出的,大多数人使用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";发布于 2014-06-19 16:49:01
大多数人通过网络发送邮件::HTTP:
use LWP::UserAgent qw( );
my $ua = LWP::UserAgent->new();
my $response = $ua->post($url, [
foo => 123,
bar => 546,
]);https://stackoverflow.com/questions/24312032
复制相似问题