2016-04-28 112 views
2

我想在我的Perl腳本讀取HTTP或HTTPS請求頭:提取頭從HTTP請求在Perl

#!/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; [email protected]$W0RD; 
X-Powered-By: adzerk bifrost/ 
x-served-by: engine-i-536776c8 

回答

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

除非我誤解了一些東西,否則我沒有看到你的問題。 您已在使用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 
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->頭($字段)

這是用於獲取標題值並從 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方法,但它是由來自HTTP::Response->as_string方法覆蓋。但HTTP::Message有兩種解決方案:

$ MESS->頭

返回嵌入式HTTP ::頭的對象。

你可以走雖然對象做得到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 

解決方法二:

$混亂 - > headers_as_string $ mess-> headers_as_string($ eol)

調用消息中標題的as_string()方法。

嘗試

print $result->headers_as_string; 

和以前一樣,你會得到完全相同的輸出。