0

我即将在这里丢失它,我正在尝试发送两个 HTTP 1.1 以获取我的一些 url 页面的源代码(我不想使用 LWP),并且只有第一个有效,即使我切换了顺序,所以理论上这两个请求都很好,这就是我所做的:

我什至创建了两个套接字以防万一,但结果相同......

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 $_;
    }

问题似乎出在哪里?

提前致谢。

4

2 回答 2

4

您使用 keep-alive 发送 HTTP/1.1 请求(这在 HTTP/1.1 中是隐式的,因此您可以跳过 Connection 标头)。但是,您不会尝试解析 HTTP 响应,而只是假设它会在请求完成后关闭连接。这是完全错误的。

我真的建议您使用已建立的 HTTP 库,例如 LWP、HTTP::Tiny ...,而不是自己尝试。如果您真的想这样做,请阅读相关标准,例如 RFC 2616,它解释了您忽略的所有重要内容:http 响应标头和正文、内容长度与分块编码、内容编码等 HTTP/1.1 不是就这么简单——如果你想要它简单,请使用 HTTP/1.0,不要使用 keep-alive。

于 2014-04-04T06:06:49.237 回答
0

我不确定,但是在第一次发送之后实例化第二个套接字时,重新实例化第一个套接字它确实有效,起初我认为这是因为我没有关闭第一个套接字,但即使这样close($socket)它也不起作用,除非我像这样使用它:

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 $_;
    }
于 2014-04-04T00:22:08.820 回答