windows 在 Perl 中发送 HTTP 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6547805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending HTTP request in Perl
提问by OOO ''MMM''
How can I send a request like this in Perl on Windows?
如何在 Windows 上的 Perl 中发送这样的请求?
GET /index.html HTTP/1.1
Host: www.example.org
Cookie: test=quest
回答by nandhp
You can do this using sockets:
您可以使用套接字执行此操作:
use IO::Socket;
my $sock = new IO::Socket::INET (
PeerAddr => 'www.example.org',
PeerPort => '80',
Proto => 'tcp',
);
die "Could not create socket: $!\n" unless $sock;
print $sock "GET /index.html HTTP/1.0\r\n";
print $sock "Host: www.example.org\r\n";
print $sock "Cookie: test=quest\r\n\r\n";
print while <$sock>;
close($sock);
but you might want to consider using LWP (libwww-perl) instead:
但您可能要考虑使用 LWP (libwww-perl) 代替:
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$req = HTTP::Request->new(GET => 'http://www.example.org/index.html');
$req->header('Cookie' => 'test=quest');
# send request
$res = $ua->request($req);
# check the outcome
if ($res->is_success) { print $res->decoded_content }
else { print "Error: " . $res->status_line . "\n" }
You might try reading the LWP cookbookfor an introduction to LWP.
您可以尝试阅读LWP 说明书以了解 LWP。
回答by Quentin
LWP::UserAgentis the normal starting point. You can pass in an HTTP::Cookiesobject manually if you want to set up specific cookie values in advance.
LWP::UserAgent是正常的起点。如果您想提前设置特定的 cookie 值,您可以手动传入HTTP::Cookies对象。