Net::HTTP(3) | User Contributed Perl Documentation | Net::HTTP(3) |
Net::HTTP - Low-level HTTP connection (client)
version 6.19
use Net::HTTP; my $s = Net::HTTP->new(Host => "www.perl.com") || die $@; $s->write_request(GET => "/", 'User-Agent' => "Mozilla/5.0"); my($code, $mess, %h) = $s->read_response_headers; while (1) { my $buf; my $n = $s->read_entity_body($buf, 1024); die "read failed: $!" unless defined $n; last unless $n; print $buf; }
The "Net::HTTP" class is a low-level HTTP client. An instance of the "Net::HTTP" class represents a connection to an HTTP server. The HTTP protocol is described in RFC 2616. The "Net::HTTP" class supports "HTTP/1.0" and "HTTP/1.1".
"Net::HTTP" is a sub-class of one of "IO::Socket::IP" (IPv6+IPv4), "IO::Socket::INET6" (IPv6+IPv4), or "IO::Socket::INET" (IPv4 only). You can mix the methods described below with reading and writing from the socket directly. This is not necessary a good idea, unless you know what you are doing.
The following methods are provided (in addition to those of "IO::Socket::INET"):
Host: Initial host attribute value KeepAlive: Initial keep_alive attribute value SendTE: Initial send_te attribute_value HTTPVersion: Initial http_version attribute value PeerHTTPVersion: Initial peer_http_version attribute value MaxLineLength: Initial max_line_length attribute value MaxHeaderLines: Initial max_header_lines attribute value
The "Host" option is also the default for "IO::Socket::INET"'s "PeerAddr". The "PeerPort" defaults to 80 if not provided. The "PeerPort" specification can also be embedded in the "PeerAddr" by preceding it with a ":", and closing the IPv6 address on brackets "[]" if necessary: "192.0.2.1:80","[2001:db8::1]:80","any.example.com:80".
The "Listen" option provided by "IO::Socket::INET"'s constructor method is not allowed.
If unable to connect to the given HTTP server then the constructor returns "undef" and $@ contains the reason. After a successful connect, a "Net:HTTP" object is returned.
The actual headers set will depend on the value of the "http_version" and "peer_http_version" attributes.
If $content is given (and it is non-empty), then a "Content-Length" header is automatically added unless it was already present.
Returns true if successful.
Returns true if successful.
As a side effect this method updates the 'peer_http_version' attribute.
Options might be passed in as key/value pairs. There are currently only two options supported; "laxed" and "junk_out".
The "laxed" option will make read_response_headers() more forgiving towards servers that have not learned how to speak HTTP properly. The "laxed" option is a boolean flag, and is enabled by passing in a TRUE value. The "junk_out" option can be used to capture bad header lines when "laxed" is enabled. The value should be an array reference. Bad header lines will be pushed onto the array.
The "laxed" option must be specified in order to communicate with pre-HTTP/1.0 servers that don't describe the response outcome or the data they send back with a header block. For these servers peer_http_version is set to "0.9" and this method returns (200, "Assumed OK").
The method will raise an exception (die) if the server does not speak proper HTTP or if the "max_line_length" or "max_header_length" limits are reached. If the "laxed" option is turned on and "max_line_length" and "max_header_length" checks are turned off, then no exception will be raised and this method will always return a response code.
The return value will be "undef" on read errors, 0 on EOF, -1 if no data could be returned this time, otherwise the number of bytes assigned to $buf. The $buf is set to "" when the return value is -1.
You normally want to retry this call if this function returns either -1 or "undef" with $! as EINTR or EAGAIN (see Errno). EINTR can happen if the application catches signals and EAGAIN can happen if you made the socket non-blocking.
This method will raise exceptions (die) if the server does not speak proper HTTP. This can only happen when reading chunked data.
length($s->_rbuf)
but might be more efficient.
The read_response_headers() and read_entity_body() will invoke the sysread() method when they need more data. Subclasses might want to override this method to control how reading takes place.
The object itself is a glob. Subclasses should avoid using hash key names prefixed with "http_" and "io_".
LWP, IO::Socket::INET, Net::HTTP::NB
Gisle Aas <gisle@activestate.com>
This software is copyright (c) 2001-2017 by Gisle Aas.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
2019-05-16 | perl v5.34.0 |