在Perl中从HTTP请求中提取头文件
我想在perl脚本中读取http或https请求中的标题:在Perl中从HTTP请求中提取头文件,perl,header,Perl,Header,我想在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-typ
#!/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
除非我有什么误解,否则我看不出你有什么问题。 您已经在使用which,它声明
->get
返回一个对象,您可以在该对象上调用->header
,以获取所记录的头字段
请注意,
$req
实际上是一个响应对象,而不是请求
要检查所有标题字段,您需要查找$resp->headers\u as\u 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";
$result
将不是纯文本响应,而是一个。上述脚本的输出为:
HTTP::Response=HASH(0x23dbfc8)
HTTP code: 200
此对象具有获取HTTP状态代码的方法(如->code
)。文件说明(简称):
$r->标题($field)
这用于获取标题值,并从中继承
通过有关详细信息和详细信息,请参阅
可用于访问标头的其他类似方法
HTTP::Headers
本身有一个方法header\u field\u name
:
$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
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
$result->header($header\u name)
对于为已知的头名称获取一个头值也很有用。让我们说一下,我们想要得到响应的ETag:
print $result->header('ETag');
HTTP::Headers
也有一个->as\u string
方法,但它被HTTP::Response
中的->as\u string
方法覆盖。但是HTTP::Message
有两种解决方案:
$mess->headers
返回嵌入的HTTP::Headers对象
您可以遍历这些对象,通过执行以下操作将HTTP头作为一个字符串获取
print $result->headers->as_string;
哪些产出:
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
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\u作为\u字符串
$mess->headers\u作为字符串($eol)
为消息中的头调用as_string()方法
试一试
您将得到与以前完全相同的输出。问题要求的是请求头,而不是响应头。
print $result->headers_as_string;