我正在嘗試編寫一個腳本,該腳本可以從遠程服務器的XML文件中收集信息。遠程服務器需要認證。由於它使用基本身份驗證,因此我能夠進行身份驗證,但由於XML文件之前的所有行,似乎無法解析數據。有沒有辦法避免所有這些行並正確解析XML文件?Perl用一些額外的行通過HTTP解析XML文件
代碼
#! /usr/bin/perl
use LWP::UserAgent;
use HTTP::Request::Common;
use XML::Simple;
$ua = LWP::UserAgent->new;
$req = HTTP::Request->new(GET => 'https://192.168.1.10/getxml?/home/');
$ua->ssl_opts(SSL_verify_mode => SSL_VERIFY_NONE); #Used to ignore certificate
$req->authorization_basic('admin', 'test');
$test = $ua->request($req)->as_string;
print $test;
# create object
my $xml = new XML::Simple;
# read XML file
my $data = $xml->XMLin("$test");
# access XML data
print $data->{status}[0]{productID};
響應
HTTP/1.1 200 OK
Connection: close
Date: Wed, 24 Sep 2014 01:12:20 GMT
Server:
Content-Length: 252
Content-Type: text/xml; charset=UTF-8
Client-Date: Wed, 24 Sep 2014 01:11:59 GMT
Client-Peer: 192.168.1.10:443
Client-Response-Num: 1
Client-SSL-Cert-Issuer: XXXXXXXXXXXX
Client-SSL-Cert-Subject: XXXXXXXXXXXXX
Client-SSL-Cipher: XXXXXXXXXXXX
Client-SSL-Socket-Class: IO::Socket::SSL
<?xml version="1.0"?>
<Status>
<SystemUnit item="1">
<ProductId item="1">TEST SYSTEM</ProductId>
</SystemUnit>
</Status>
:1: parser error : Start tag expected, '<' not found
HTTP/1.1 200 OK
而且,你猜對了,'$ test = $ ua-> request($ req) - > header'只會返回頭文件。參見[HTTP :: Request](https://metacpan.org/pod/release/GAAS/HTTP-Message-6.06/lib/HTTP/Request.pm) – dwarring 2014-09-24 04:12:18