使用 LWP 发送纯字符串请求

发布于 2024-11-04 17:24:25 字数 1004 浏览 0 评论 0原文

为了从某个网站获得响应,我必须给出一个确切的请求字符串,HTTP/1.1。我尝试了 telnet,它给了我想要的响应(重定向,但我需要它)。

但是,当我尝试向 HTTP::Request->parse() 提供相同的请求字符串时,我只收到消息 400 URL 必须是绝对的

我不确定是网站还是 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";

有人可以帮助我吗?

To get a response from a certain website, I have to give one exact request string, HTTP/1.1. I tried that one with telnet, it gives me the response I want (a redirect, but I need it).

But when I try to give the same request string to HTTP::Request->parse(), I merely get the message 400 URL must be absolute.

I am not sure if it's the website or LWP giving me that, because as I said, the response worked with telnet.

This is the code:

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";

Anyone can help me here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凑诗 2024-11-11 17:24:26

好吧,我是用 Sockets 做到的。毕竟,我有 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 结束。

Ok, I did it using Sockets. After all, I had the HTTP request and wanted the plain response. Here the code for people who are interested:

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;

It's just important to remember to leave the while, as the HTTP response won't end with an EOF.

蓝海似她心 2024-11-11 17:24:26

在我看来,解析请求并不是 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;

It looks to me like parsing the request isn't 100 % round-trip-safe, meaning you cannot feed the response back into a request.

Looks like a bug at first sight, but the module's been out for such a long time… On the other hand, I didn't even know you could use this module to parse a request, so maybe it's not so well tested.

The following test case should point you to the problem, which is that the URL isn't properly assembled for being fed to the $req->request method.

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;
无法回应 2024-11-11 17:24:25

如果没有指定架构,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";

LWP::UserAgent dies with the error you are getting if there is no schema specified in request. It probably need it to properly work with it.

So, to make it work, you need to specify full url for your request:

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文