#!/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;
和以前一樣,你會得到完全相同的輸出。