使用 LWP 发送纯字符串请求
为了从某个网站获得响应,我必须给出一个确切的请求字符串,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我是用 Sockets 做到的。毕竟,我有 HTTP 请求并且想要简单的响应。这里是感兴趣的人的代码:
记住保留
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:
It's just important to remember to leave the
while
, as the HTTP response won't end with anEOF
.在我看来,解析请求并不是 100% 往返安全,这意味着您无法将响应反馈回请求中。
乍一看像是一个错误,但该模块已经发布了很长时间......另一方面,我什至不知道你可以使用这个模块来解析请求,所以也许它没有经过很好的测试。
以下测试用例应该会指出问题所在,即 URL 未正确组装以提供给
$req->request
方法。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.如果没有指定架构,LWP::UserAgent 会因您收到的错误而终止在请求中。它可能需要它才能正常工作。
因此,要使其正常工作,您需要为您的请求指定完整的 url:
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: