加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows – 在Perl中发送HTTP请求

发布时间:2020-12-14 05:46:21 所属栏目:Windows 来源:网络整理
导读:如何在 Windows上的Perl中发送这样的请求? GET /index.html HTTP/1.1Host: www.example.orgCookie: test=quest 解决方法 您可以使用套接字执行此操作: use IO::Socket;my $sock = new IO::Socket::INET ( PeerAddr = 'www.example.org',PeerPort = '80',Pr
如何在 Windows上的Perl中发送这样的请求?

GET /index.html HTTP/1.1
Host: www.example.org
Cookie: test=quest

解决方法

您可以使用套接字执行此操作:

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.0rn";
print $sock "Host: www.example.orgrn";
print $sock "Cookie: test=questrnrn";
print while <$sock>;
close($sock);

但你可能想考虑使用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" }

您可以尝试阅读LWP cookbook以获取LWP简介.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读