NAME
Net::HTTP - Low-level HTTP client connection
NOTE
This module is experimental. Details of its interface is likely to change in the future.
SYNOPSIS
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);
last unless $n;
print $buf;
}
DESCRIPTION
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.
Net::HTTP
is a sub-class of IO::Socket::INET
. You can mix the methods described below with reading and writing from the socket directly.
The following methods are provided (in addition to those of IO::Socket::INET
):
- $s = Net::HTTP->new( %options )
-
The
Net::HTTP
constructor takes the same options asIO::Socket::INET
as well as these:Host: Initial host attribute value KeepAlive: Initial keep_alive attribute value HTTPVersion: Initial http_version attribute value PeerHTTPVersion: Initial peer_http_version attribute value
- $s->host
-
Get/set the default value of the
Host
header to send. The $host should not be set to an empty string (orundef
). - $s->keep_alive
-
Get/set the keep-alive value. If this value is TRUE then the request will sendt with headers indicating that the server should try to keep the connection open so that multiple requests can be sent.
The actual headers set will depend on the value of the
http_version
andpeer_http_version
attributes. - $s->http_version
-
Get/set the HTTP version number that this client should announce. This value can only be set to "1.0" or "1.1". The default is "1.1".
- $s->peer_http_version
-
Get/set the protocol version number of our peer. This value will initially be "1.0", but will be updated by a successful read_response_headers() method call.
- $s->format_request($method, $uri, %headers, [$content])
-
Format a request message and return it as a string. If the headers do not include a
Host
header, then a header is inserted with the value of thehost
attribute. Headers likeConnection
andKeep-Alive
might also be added depending on the status of thekeep_alive
attribute.If $content is given (and it is non-empty), then a
Content-Length
header is automatically added unless it was already present. - $s->write_request($method, $uri, %headers, [$content])
-
Format and send a request message. Arguments are the same as for format_request(). Returns true if successful.
- $s->write_chunk($data)
-
Will write a new chunk of request entity body data. This method should only be used if the
Transfer-Encoding
header with a value ofchunked
was sent in the request. Note, writing zero-length data is a no-op. Use the write_chunk_eof() method to signal end of entity body data.Returns true if successful.
- $s->write_chunk_eof(%trailers)
-
Will write eof marker for chunked data and optional trailers. Note that trailers should not really be used unless is was signaled with a
Trailer
header.Returns true if successful.
- ($code, $mess, %headers) = $s->read_response_headers
-
Read response headers from server.
- $n = $s->read_entity_body($buf, $size);
-
Reads chunks of the entity body content. Basically the same interface as for read() and sysread(), but buffer offset is not supported yet. This method should only be called after a successful read_response_headers() call.
- %headers = $s->get_trailers
-
After read_entity_body() has returned 0 to indicate end of the entity body, you might call this method to pick up any trailers.
- $s->_rbuf
-
Get/set the read buffer content. The read_response_headers() and read_entity_body() methods use an internal buffer which they will look for data before they actually sysread more from the socket itself. If they read too much, the remaining data will be left in this buffer.
- $s->_rbuf_length
-
Returns the number of bytes in the read buffer.
SUBCLASSING
The read_response_headers() and read_entity_body() will invoke the method xread() when they need more data. This method takes the same arguments as sysread() and the is in fact implemented as a call to sysread(). Subclasses might want to override this method to contol how reading takes place.
The object itself is a glob. Subclasses should avoid using hash key names prefixed with http_
and io_
.
SEE ALSO
LWP, IO::Socket::INET, Net::HTTP::NB
COPYRIGHT
Copyright 2001 Gisle Aas.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.