Question

I am about to lose it here, i am trying to send two HTTP 1.1 to get the source code of some of my url pages (I dont want to use LWP), and only the first one works, even when i switch the order, so in theory both requests are fine, here is what i do :

I even created two sockets just in case, but same result ...

my $sock =  IO::Socket::INET->new(
                PeerAddr => $dom,
                PeerPort => 'http(80)',
                Proto    => 'tcp'
            )
            or die "Could not connect to :80!! $!";

my $sock2 =     IO::Socket::INET->new(
                PeerAddr => $dom,
                PeerPort => 'http(80)',
                Proto    => 'tcp'
            )
            or die "Could not connect to :80!! $!";         


my $req2 = << 'EOT'
POST / HTTP/1.1
Host: $dom
Connection: keep-alive
Content-Length: 57
Cache-Control: max-age=0
Origin: ${org}
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: ${ref}


EOT
;




my $req = << 'EOT'
POST / HTTP/1.1
Host: $dom
Connection: keep-alive
Content-Length: 57
Cache-Control: max-age=0
Origin: ${org}
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: ${ref}


EOT
;           



$sock2->send($req2);


sleep 5;


my $abc;
while(<$sock2>) {
        print $_;
    }


$sock->send($req);


sleep 5;


while(<$sock>) {
        print $_;
    }

Where seems to be the problem?

Thanks in advance.

Était-ce utile?

La solution

You send a HTTP/1.1 request with keep-alive (which is implicit with HTTP/1.1, so you could skip the Connection header). But, you don't make any attempts to parse the HTTP response, but just assume that it will close the connection when the request is done. This is plain wrong.

I really recommend you to use established HTTP libraries like LWP, HTTP::Tiny ... and not try it on your own. If you really want to do it please read the relevant standard, e.g. RFC 2616 which explains all the important stuff you simply ignore: http response header and body, content-length vs. chunked encoding, content-encodings etc. HTTP/1.1 is not that simple - if you want it simple use HTTP/1.0 and do not use keep-alive.

Autres conseils

I am not sure, but when instantiating the second socket just after the first send, re-instantiating the first socket it does work, at first i thought it was because i didnt close the first socket, but even with that close($socket) it doesnt work unless i use it like this :

my $sock =  IO::Socket::INET->new(
                PeerAddr => $dom,
                PeerPort => 'http(80)',
                Proto    => 'tcp'
            )
            or die "Could not connect to :80!! $!";


my $req2 = <<"EOT"
POST / HTTP/1.1
Host: $dom
Connection: keep-alive
Content-Length: 57
Cache-Control: max-age=0
Origin: ${org}
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: ${ref}


EOT
;




my $req = <<"EOT"
POST / HTTP/1.1
Host: $dom
Connection: keep-alive
Content-Length: 57
Cache-Control: max-age=0
Origin: ${org}
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: ${ref}


EOT
;           



my $sock2 =     IO::Socket::INET->new(
                PeerAddr => $dom,
                PeerPort => 'http(80)',
                Proto    => 'tcp'
            )
            or die "Could not connect to :80!! $!";         

$sock2->send($req2);


sleep 5;


my $abc;
while(<$sock2>) {
        print $_;
    }


$sock->send($req);


sleep 5;


while(<$sock>) {
        print $_;
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top