2011-05-02 39 views
0

要得到某個網站的響應,我必須給出一個確切的請求字符串,HTTP/1.1。我用telnet試了一下,它給了我想要的回覆(重定向,但我需要它)。發送一個簡單的字符串請求與LWP

但是,當我試圖給相同的請求字符串HTTP::Request->parse(),我只是得到消息400 URL must be absolute

我不確定是否這是網站或LWP給了我,因爲正如我所說,響應與telnet合作。

這是代碼:

my $req = "GET/HTTP/1.1\n". 
    "Host: www.example-site.de\n". 
    "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1\n". 
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n". 
    "Accept-Language: en-us,en;q=0.5\n". 
    "Accept-Encoding: gzip, deflate\n". 
    "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n". 
    "Keep-Alive: 115\n". 
    "Connection: keep-alive\n"; 

# Gives correct request string 
print HTTP::Request->parse($req)->as_string; 

my $ua = LWP::UserAgent->new(cookie_jar => {}, agent => ''); 
my $response = $ua->request(HTTP::Request->parse($req)); 

# 400 error 
print $response->as_string,"\n"; 

任何人都可以幫助我在這裏?

回答

0

好吧,我使用套接字的確如此。畢竟,我有HTTP請求,並希望得到明確的迴應。這裏的代碼的人誰是感興趣:

use IO::Sockets; 

my $sock = IO::Socket::INET->new(
    PeerAddr => 'www.example-site.de', 
    PeerPort => 80, 
    Proto => 'Tcp', 
); 
die "Could not create socket: $!\n" unless $sock; 

print $sock, $req; 

while(<$sock>) { 
    # Look for stuff I need 
} 

close $sock; 

重要的是要記住離開while,作爲HTTP響應不會有EOF結束只是重要的。

0

它在我看來像解析請求不是100%往返安全,這意味着你不能將響應反饋回請求。

看起來像一見鍾情,但模塊已經很長時間了......另一方面,我甚至不知道你可以使用這個模塊來解析請求,所以也許它不是很好測試。

下面的測試案例應該指出您的問題,即該URL沒有正確組裝以供應給$req->request方法。

use strict; 
use warnings; 
use LWP::UserAgent; 
use HTTP::Request; 
use Test::More; 

my $host = 'www.example.com'; 
my $url = '/bla.html'; 

my $req = <<"EOS"; 
GET $url HTTP/1.1 
Host: $host 
EOS 

# (1) parse the request 
my $reqo = HTTP::Request->parse($req); 
isa_ok $reqo, 'HTTP::Request'; 
diag explain $reqo; 
diag $reqo->as_string; 

# (2) construct the request 
my $reqo2 = HTTP::Request->new(GET => "http://$host$url"); 
isa_ok $reqo2, 'HTTP::Request'; 
diag explain $reqo2; 
diag $reqo2->as_string; 

is $reqo->uri, $reqo2->uri, 'both URLs are identical'; 

my $ua = LWP::UserAgent->new(cookie_jar => {}, agent => ''); 
for ($reqo, $reqo2) { 
    my $response = $ua->request($_); 
    diag $response->as_string,"\n"; 
} 

done_testing; 
1

LWP::UserAgent如果在請求中沒有指定模式,則會因您遇到的錯誤而死亡。它可能需要它來正確地使用它。

因此,要使其工作,你需要指定完整的URL爲您的要求:

my $req_str = "GET http://www.example.de/\n". 
    "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1\n". 
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n". 
    "Accept-Language: en-us,en;q=0.5\n". 
    "Accept-Encoding: gzip, deflate\n". 
    "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n". 
    "Keep-Alive: 115\n". 
    "Connection: keep-alive\n"; 
+0

但我不能,因爲服務器不會接受我的請求。它只接受我上面給出的形狀的請求。 – Lanbo 2011-05-02 14:59:24

+0

@Scán - 如果服務器不靈活,可能唯一的方法就是直接通過套接字或者[Net :: Telnet](http://search.cpan.org/perldoc?Net :: Telnet) – bvr 2011-05-02 16:26:53

+0

@Scán:你怎麼知道服務器不接受請求?它應該,如果'parse()'不是越野車。 – reinierpost 2011-05-03 08:49:07

相關問題