我写了一个连接到URL的小脚本,发布了一些xml数据并等待响应,到目前为止一切都很好,我需要设置超时但我认为我做错了因为我收到此错误消息:
无法通过包“HTTP :: Headers”找到对象方法“timeout” /usr/share/perl5/HTTP/Message.pm第644行。
这是我的小代码:
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
my $msg =<< "EOT";
<?xml version="1.0" encoding="utf-8"?>
EOT
my $uri = "http://www.myhostexample.com/script";
my $request = HTTP::Request->new(POST => $uri);
$request->content_type("text/xml");
$request->content($msg);
my $response = LWP::UserAgent->new->request($request);
$response->timeout(10);
#print $response->content . "\n"; # debug
if ($response->content =~ /<?xml/s) {
print "OK!\n";
}
else {
print "BAD!\n";
}
答案 0 :(得分:1)
您需要为LWP :: UserAgent设置超时。
...
my $ua = LWP::UserAgent->new();
$ua->timeout(10);
my $response = $ua->request($request);
...