2012-11-21 2 views
3

LWP로 작성된 HTTP 요청에서 수정되지 않은 원시 응답 헤더를 가져올 방법이 있습니까? 이는 잘못된 형식의 헤더로 인한 문제를 식별해야하는 진단 도구를위한 것입니다.LWP에서 원시 응답 헤더를 가져 옵니까?

use LWP::UserAgent; 
my $ua = new LWP::UserAgent; 
my $response = $ua->get("http://somedomain.com"); 
print $response->headers()->as_string(); 

그러나 이것은 실제로 헤더를 구문 분석하고 분석 된 데이터로부터 이들의 정규화, 청소 업 버전을 재구성 :

내가 찾은 가장 가까운 것입니다. 전체 헤더 텍스트가 서버에서 반환 된 양식의 정확한 형식이어야하므로 형식이 잘못되었거나 표준이 아닌 모든 것이 명확하게 식별 될 수 있습니다.

LWP로 이것을 수행 할 방법이 없다면, 이것을 할 수있는 다른 펄 모듈이 있습니까?

답변

6

Net::HTTP 적은 처리와 낮은 레벨의 액세스를 제공한다. IO::Socket::INET의 하위 클래스이므로 요청을 한 후에 객체에서 직접 읽을 수 있습니다.

use Net::HTTP; 

# Make the request using Net::HTTP. 
my $s = Net::HTTP->new(Host => "www.perl.com") || die [email protected]; 
$s->write_request(GET => "/", 'User-Agent' => "Mozilla/5.0"); 

# Read the raw headers. 
my @headers; 
while(my $line = <$s>) { 
    # Headers are done on a blank line. 
    last unless $line =~ /\S/; 
    push @headers, $line; 
} 
print @headers; 
2

HTTP::Response 개체 (및 해당 개체에 포함 된 HTTP::Headers 개체)의 검사에 따르면 헤더는 구문 분석 될 때 폐기됩니다.

대신 WWW::Curl을 시도하는 것이 좋습니다. WWW를 사용

EDIT 니핏 :: 컬 :

use WWW::Curl::Easy; 

my ($header, $body); 

my $curl = WWW::Curl::Easy->new; 
$curl->setopt(CURLOPT_URL, $url_to_get); # get this URL 
$curl->setopt(CURLOPT_WRITEHEADER, \$header); # save header text in this var 
$curl->setopt(CURLOPT_WRITEDATA, \$body); # save body text in this var 

my $code = $curl->perform; 
if (0 == $code) { 
    # header text is in $header, body text in $body 
} else { 
    print $curl->strerror($code).": ".$curl->errbuf."\n"; 
} 
관련 문제