我想在我的perl脚本中读取http或https请求中的头:
#!/usr/bin/perl
use LWP::UserAgent;
my $ua = new LWP::UserAgent;
my $url = "http://example.com/";
my $req = $ua->get("$url");我希望提取上请求的头数据,例如:
HTTP/1.1 200 OK
Access-Control-Allow-Headers: accept, origin, content-type, content-length
Access-Control-Allow-Methods: GET,PUT,POST,DELETE,OPTIONS
Access-Control-Allow-Origin: *
Content-Encoding: gzip
Content-Type: application/javascript
Date: Thu, 28 Apr 2016 01:43:01 GMT
Etag: "779429019"
Cookie: username=usrname; pass=P@S$W0RD;
X-Powered-By: adzerk bifrost/
x-served-by: engine-i-536776c8发布于 2016-04-28 02:57:02
请注意,您的$req实际上是响应对象,而不是请求。
要检查所有标头字段,您需要查找$resp->headers_as_string
要生成显示的输出,您可以编写以下代码
#!/usr/bin/perl
use LWP::UserAgent;
my $ua = new LWP::UserAgent;
my $url = 'http://example.com/';
my $resp = $ua->get($url);
print $resp->protocol, ' ', $resp->status_line, "\n";
print $resp->headers_as_string, "\n";发布于 2016-04-28 02:17:01
除非我发现了什么,否则我看不出你有什么问题。您已经在使用LWP::UserAgent,它声明->get返回一个HTTP::Respnse对象,您可以在该对象上调用->header获取头部字段,作为文档化的这里。
use v5.12;
use warnings;
use LWP::UserAgent;
my $ua = new LWP::UserAgent;
my $url = "http://github.com/";
my $res = $ua->get("$url");
say "Headers returned: ", join(", ", $res->header_field_names);
say "With values:" ;
say " $_: ", $res->header($_) for $res->header_field_names ;
# Outputs
# Headers returned: Content-Type, Client-Date, Client-Warning
# With values:
# Content-Type: text/plain
# Client-Date: Thu, 28 Apr 2016 02:31:26 GMT
# Client-Warning: Internal response发布于 2016-04-28 05:50:38
#!/usr/bin/perl -l
use strict;
use warnings;
use LWP::UserAgent;
my $ua = new LWP::UserAgent;
my $result = $ua->get("http://example.com/");
print $result;
print "HTTP code: ".$result->code;$result将不是纯文本响应,而是HTTP::响应对象。以上脚本的输出如下:
HTTP::Response=HASH(0x23dbfc8)
HTTP code: 200这个对象有一些方法(比如获取HTTP代码的->code )。文件规定(简称):
$r->头( $field ) 它用于获取标头值,并通过HTTP::Header通过HTTP::Message继承。有关可用于访问标头的详细信息和其他类似方法,请参见HTTP::Header。
HTTP::Headers本身有一个方法header_field_names
$h->头字段名 返回标题中存在的字段的不同名称列表。字段名具有HTTP规范建议的大小写,名称按推荐的“良好实践”顺序返回。 在标量上下文中,返回不同字段名的数目。
您的脚本可以轻松地获得所请求的信息:
for my $header_name ($result->header_field_names) {
print $header_name.": ".$result->header($header_name);
}其中产出:
Cache-Control: max-age=604800
Connection: close
Date: Thu, 28 Apr 2016 05:40:52 GMT
ETag: "359670651+ident"
Server: ECS (iad/182A)
Vary: Accept-Encoding
Content-Length: 1270
Content-Type: text/html
Expires: Thu, 05 May 2016 05:40:52 GMT
Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
Client-Date: Thu, 28 Apr 2016 05:40:52 GMT
Client-Peer: 2606:2800:220:1:248:1893:25c8:1946:80
Client-Response-Num: 1
Title: Example Domain
X-Cache: HIT
X-Ec-Custom-Error: 1
X-Meta-Charset: utf-8
X-Meta-Viewport: width=device-width, initial-scale=1$result->header($header_name)对于获取一个已知头名的标头值也很有用。假设我们希望得到响应的ETag:
print $result->header('ETag');HTTP::Headers也有一个->as_string方法,但是它被来自HTTP::Response的->as_string方法覆盖。但是HTTP::Message有两个解决方案:
$杂乱->标头 返回嵌入的HTTP::Header对象。
您可以遍历这些对象,将HTTP头作为一个字符串来获取。
print $result->headers->as_string;其中产出:
Cache-Control: max-age=604800
Connection: close
Date: Thu, 28 Apr 2016 05:47:54 GMT
ETag: "359670651+ident"
Server: ECS (iad/182A)
Vary: Accept-Encoding
Content-Length: 1270
Content-Type: text/html
Expires: Thu, 05 May 2016 05:47:54 GMT
Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
Client-Date: Thu, 28 Apr 2016 05:47:54 GMT
Client-Peer: 2606:2800:220:1:248:1893:25c8:1946:80
Client-Response-Num: 1
Title: Example Domain
X-Cache: HIT
X-Ec-Custom-Error: 1
X-Meta-Charset: utf-8
X-Meta-Viewport: width=device-width, initial-scale=1第二种解决办法:
$mess>headers_as_string$mess>headers_as_string( $eol ) 为消息中的标头调用as_string()方法。
试一试
print $result->headers_as_string;你会得到和以前完全一样的输出。
https://stackoverflow.com/questions/36903905
复制相似问题