【问题标题】:How can I log the HTTP response codes in the file in Perl?如何在 Perl 的文件中记录 HTTP 响应代码?
【发布时间】:2011-01-30 05:09:23
【问题描述】:

我创建了如下所示的 HTTP::Request:

 #!/usr/bin/perl
require HTTP::Request;
require LWP::UserAgent;
require HTTP::Cookies;

$request = HTTP::Request->new(GET => 'http://www.google.com/');
$ua = LWP::UserAgent->new;
$cookie_jar = HTTP::Cookies->new();
$ua->cookie_jar($cookie_jar);
$cookie_jar->set_cookie(0,'testCookie','cookieValue','/','http://www.google.com/',80,0,0,86400,0);

$response = $ua->request($request);
if($response->is_success){
print "sucess\n";
print $response->code;
print "\n";
}
else {
print "fail\n";
die $response->code;
print "\n";
}

现在,当我发送请求时: 我想在文件中记录 http 响应代码

请帮忙 谢谢你

【问题讨论】:

  • @David Dorward:这可能是一个不同的问题,但加入了您提供的问题。没有给出详细的答案,所以我把这个分开以获得具体的答案希望你能理解
  • 看到 $response->code; 好吧,与其死去,不如去任何你想要的地方记录。

标签: perl file


【解决方案1】:

如果要打印到文件,则打印到文件:

 open my($log), '>', $log_file or die "Could not open $log_file: $!";

 ....

 if($response->is_success){
     print $log "success:", $response->code, "\n";
 }
 else {
     print $log "fail: ", $response->code, "\n";
 }

解决这类问题的一般方法是找出您想要做什么,然后通过查看文档或 Perl 教程了解如何执行此操作。大多数你想做的事情只是结合你在Learning Perl中找到的基础知识。

【讨论】:

  • 当我使用上面的代码时给了我以下消息:无法打开 $log 文件我需要在某处创建文件吗?请详细说明
  • 正如我在上一句中所说的,您可能想通过 Learning Perl
猜你喜欢
  • 2014-09-16
  • 2023-03-10
  • 2016-10-17
  • 2014-01-23
  • 2018-12-07
  • 1970-01-01
  • 1970-01-01
  • 2020-11-12
相关资源
最近更新 更多