2011-09-05 72 views
0

我有一臺服務器向我發送此響應。然而,使用下面的代碼,我最終得到了一個不包含任何部分的實體,以及作爲MIME :: Body的邊界(包含)之間的所有內容。有沒有什麼我可以做的短實施我自己的多部分分析器(通常可能會有更多的部分),並廢除應該爲我做的模塊?MIME ::解析器無法正確解析多部分/混合部分

#!/usr/bin/perl 

use MIME::Parser; 

my $response = <<_EOF; 
HTTP/1.1 200 OK 
Transfer-Encoding: chunked 
Content-Type: multipart/mixed; boundary="be4dc417ebd640944ab26f033e5ea1ab" 

--be4dc417ebd640944ab26f033e5ea1ab 
Content-Type: application/json 

{"a":"b"} 
--be4dc417ebd640944ab26f033e5ea1ab-- 
_EOF 

my $mime_parser = new MIME::Parser; 
$mime_parser->tmp_to_core(1); 
$mime_parser->output_to_core(1); 
my $entity = $mime_parser->parse_data($response); 

print "$MIME::Parser::VERSION $^V $^O\n\n"; 
$entity->print(\*STDOUT); 
print "\n\n"; 
print $entity->parts(0)->bodyhandle->as_string; 

輸出:

5.502 v5.10.1 MSWin32 


--be4dc417ebd640944ab26f033e5ea1ab 
Content-Type: application/json 

{"a":"b"} 
--be4dc417ebd640944ab26f033e5ea1ab-- 


Can't call method "bodyhandle" on an undefined value at test.pl line 25. 
+1

對我來說,'$ entity-> parts(0)'返回一個MIME :: Entity實例,它的主體只是'{「a」:「b」}',所以發佈一個可以獨立運行的完整程序正確地展示你的問題。 - '$ entity-> bodyhandle-> as_string'在未定義的值上拋出錯誤'Can not call method「as_string」,這是一個編程錯誤,你需要'$ entity-> parts(0) - > bodyhandle - > as_string'。 – daxim

+0

嗯。將明天檢查我的版本併發布完整的程序。 – OrangeDog

回答

1

HTTP/1.1 200 OK的線不是MIME響應的一部分。拿出來,它工作正常。