从Perl中的HTTP请求中提取标头

时间:2016-04-28 01:53:46

标签: 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-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

3 个答案:

答案 0 :(得分:2)

请注意,您的$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";

答案 1 :(得分:1)

除非我误解了什么,否则我不会看到你的问题。 您已经在使用LWP::UserAgent,其中指出->get会返回一个HTTP::Respnse对象,您可以在该对象上调用->header来获取标题字段here

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

答案 2 :(得分:0)

#!/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::Response object。上述脚本的输出是:

HTTP::Response=HASH(0x23dbfc8)
HTTP code: 200

此对象具有获取HTTP状态代码的方法(如->code)。文档陈述(缩写):

  

$ r->标头($ field)

     

这用于获取标头值,它继承自   HTTP::Headers通过HTTP::Message。有关详细信息,请参阅HTTP::Headers   其他可用于访问标题的类似方法。

HTTP::Headers本身有一个方法header_field_names

  

$ H-> header_field_names

     

返回。中存在的字段的不同名称列表   头。字段名称具有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方法,但它被->as_string中的HTTP::Response方法覆盖。但是HTTP::Message有两个解决方案:

  

$ MESS->标头

     

返回嵌入的HTTP :: Headers对象。

您可以通过执行

来遍历对象以将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;

你将获得与以前完全相同的输出。