Save socket response to string in Perl

I am trying to connect to https url to save the response to a string.

use strict;
use warnings;
use IO::Socket::SSL;

my $socket = IO::Socket::SSL->new(
   PeerHost => "google.com",
   PeerPort => "https"
) or die "Error: $!";
print $socket "GET / HTTP/1.0\r\n\r\n";
my $content = <$socket>;
print $content;
print "length: ";
print length($content);
print "\n";
close $socket;

The output is just the first line of the HTTP response:

HTTP/1.0 302 Found
length: 20

When I modify the script and print the answer with "print", the output is the complete answer:

use strict;
use warnings;
use IO::Socket::SSL;

my $socket = IO::Socket::SSL->new(
   PeerHost => "google.com",
   PeerPort => "https"
) or die "Error: $!";
print $socket "GET / HTTP/1.0\r\n\r\n";
print <$socket>;
close $socket;

Output:

HTTP/1.0 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: https://www.google.de/?gfe_rd=cr&ei=XAroVeuYDMWo8wfioYKQBw
Content-Length: 259
Date: Thu, 03 Sep 2015 08:52:44 GMT
Server: GFE/2.0
Alternate-Protocol: 443:quic,p=1
Alt-Svc: quic=":443"; p="1"; ma=604800

<HTML>...</HTML>

I do not understand why I cannot save the socket response to the string.

I am using Perl v5.14.2

+4
source share
1 answer

Chris Doyle's comment is the solution to this problem:

<$socket> - . . . Print - , . - 10

my $content = join('',<$socket>);. <$socket> , , . - 8

+2

Source: https://habr.com/ru/post/1605704/


All Articles