1Net::HTTP(3)          User Contributed Perl Documentation         Net::HTTP(3)
2
3
4

NAME

6       Net::HTTP - Low-level HTTP connection (client)
7

SYNOPSIS

9        use Net::HTTP;
10        my $s = Net::HTTP->new(Host => "www.perl.com") || die $@;
11        $s->write_request(GET => "/", 'User-Agent' => "Mozilla/5.0");
12        my($code, $mess, %h) = $s->read_response_headers;
13
14        while (1) {
15           my $buf;
16           my $n = $s->read_entity_body($buf, 1024);
17           die "read failed: $!" unless defined $n;
18           last unless $n;
19           print $buf;
20        }
21

DESCRIPTION

23       The "Net::HTTP" class is a low-level HTTP client.  An instance of the
24       "Net::HTTP" class represents a connection to an HTTP server.  The HTTP
25       protocol is described in RFC 2616.  The "Net::HTTP" class supports
26       "HTTP/1.0" and "HTTP/1.1".
27
28       "Net::HTTP" is a sub-class of "IO::Socket::INET".  You can mix the
29       methods described below with reading and writing from the socket
30       directly.  This is not necessary a good idea, unless you know what you
31       are doing.
32
33       The following methods are provided (in addition to those of
34       "IO::Socket::INET"):
35
36       $s = Net::HTTP->new( %options )
37           The "Net::HTTP" constructor method takes the same options as
38           "IO::Socket::INET"'s as well as these:
39
40             Host:            Initial host attribute value
41             KeepAlive:       Initial keep_alive attribute value
42             SendTE:          Initial send_te attribute_value
43             HTTPVersion:     Initial http_version attribute value
44             PeerHTTPVersion: Initial peer_http_version attribute value
45             MaxLineLength:   Initial max_line_length attribute value
46             MaxHeaderLines:  Initial max_header_lines attribute value
47
48           The "Host" option is also the default for "IO::Socket::INET"'s
49           "PeerAddr".  The "PeerPort" defaults to 80 if not provided.
50
51           The "Listen" option provided by "IO::Socket::INET"'s constructor
52           method is not allowed.
53
54           If unable to connect to the given HTTP server then the constructor
55           returns "undef" and $@ contains the reason.  After a successful
56           connect, a "Net:HTTP" object is returned.
57
58       $s->host
59           Get/set the default value of the "Host" header to send.  The $host
60           must not be set to an empty string (or "undef") for HTTP/1.1.
61
62       $s->keep_alive
63           Get/set the keep-alive value.  If this value is TRUE then the
64           request will be sent with headers indicating that the server should
65           try to keep the connection open so that multiple requests can be
66           sent.
67
68           The actual headers set will depend on the value of the
69           "http_version" and "peer_http_version" attributes.
70
71       $s->send_te
72           Get/set the a value indicating if the request will be sent with a
73           "TE" header to indicate the transfer encodings that the server can
74           choose to use.  If the "Compress::Zlib" module is installed then
75           this will announce that this client accepts both the deflate and
76           gzip encodings.
77
78       $s->http_version
79           Get/set the HTTP version number that this client should announce.
80           This value can only be set to "1.0" or "1.1".  The default is
81           "1.1".
82
83       $s->peer_http_version
84           Get/set the protocol version number of our peer.  This value will
85           initially be "1.0", but will be updated by a successful
86           read_response_headers() method call.
87
88       $s->max_line_length
89           Get/set a limit on the length of response line and response header
90           lines.  The default is 8192.  A value of 0 means no limit.
91
92       $s->max_header_length
93           Get/set a limit on the number of header lines that a response can
94           have.  The default is 128.  A value of 0 means no limit.
95
96       $s->format_request($method, $uri, %headers, [$content])
97           Format a request message and return it as a string.  If the headers
98           do not include a "Host" header, then a header is inserted with the
99           value of the "host" attribute.  Headers like "Connection" and
100           "Keep-Alive" might also be added depending on the status of the
101           "keep_alive" attribute.
102
103           If $content is given (and it is non-empty), then a "Content-Length"
104           header is automatically added unless it was already present.
105
106       $s->write_request($method, $uri, %headers, [$content])
107           Format and send a request message.  Arguments are the same as for
108           format_request().  Returns true if successful.
109
110       $s->format_chunk( $data )
111           Returns the string to be written for the given chunk of data.
112
113       $s->write_chunk($data)
114           Will write a new chunk of request entity body data.  This method
115           should only be used if the "Transfer-Encoding" header with a value
116           of "chunked" was sent in the request.  Note, writing zero-length
117           data is a no-op.  Use the write_chunk_eof() method to signal end of
118           entity body data.
119
120           Returns true if successful.
121
122       $s->format_chunk_eof( %trailers )
123           Returns the string to be written for signaling EOF when a
124           "Transfer-Encoding" of "chunked" is used.
125
126       $s->write_chunk_eof( %trailers )
127           Will write eof marker for chunked data and optional trailers.  Note
128           that trailers should not really be used unless is was signaled with
129           a "Trailer" header.
130
131           Returns true if successful.
132
133       ($code, $mess, %headers) = $s->read_response_headers( %opts )
134           Read response headers from server and return it.  The $code is the
135           3 digit HTTP status code (see HTTP::Status) and $mess is the
136           textual message that came with it.  Headers are then returned as
137           key/value pairs.  Since key letter casing is not normalized and the
138           same key can even occur multiple times, assigning these values
139           directly to a hash is not wise.  Only the $code is returned if this
140           method is called in scalar context.
141
142           As a side effect this method updates the 'peer_http_version'
143           attribute.
144
145           Options might be passed in as key/value pairs.  There are currently
146           only two options supported; "laxed" and "junk_out".
147
148           The "laxed" option will make read_response_headers() more forgiving
149           towards servers that have not learned how to speak HTTP properly.
150           The "laxed" option is a boolean flag, and is enabled by passing in
151           a TRUE value.  The "junk_out" option can be used to capture bad
152           header lines when "laxed" is enabled.  The value should be an array
153           reference.  Bad header lines will be pushed onto the array.
154
155           The "laxed" option must be specified in order to communicate with
156           pre-HTTP/1.0 servers that don't describe the response outcome or
157           the data they send back with a header block.  For these servers
158           peer_http_version is set to "0.9" and this method returns (200,
159           "Assumed OK").
160
161           The method will raise an exception (die) if the server does not
162           speak proper HTTP or if the "max_line_length" or
163           "max_header_length" limits are reached.  If the "laxed" option is
164           turned on and "max_line_length" and "max_header_length" checks are
165           turned off, then no exception will be raised and this method will
166           always return a response code.
167
168       $n = $s->read_entity_body($buf, $size);
169           Reads chunks of the entity body content.  Basically the same
170           interface as for read() and sysread(), but the buffer offset
171           argument is not supported yet.  This method should only be called
172           after a successful read_response_headers() call.
173
174           The return value will be "undef" on read errors, 0 on EOF, -1 if no
175           data could be returned this time, otherwise the number of bytes
176           assigned to $buf.  The $buf is set to "" when the return value is
177           -1.
178
179           You normally want to retry this call if this function returns
180           either -1 or "undef" with $! as EINTR or EAGAIN (see Errno).  EINTR
181           can happen if the application catches signals and EAGAIN can happen
182           if you made the socket non-blocking.
183
184           This method will raise exceptions (die) if the server does not
185           speak proper HTTP.  This can only happen when reading chunked data.
186
187       %headers = $s->get_trailers
188           After read_entity_body() has returned 0 to indicate end of the
189           entity body, you might call this method to pick up any trailers.
190
191       $s->_rbuf
192           Get/set the read buffer content.  The read_response_headers() and
193           read_entity_body() methods use an internal buffer which they will
194           look for data before they actually sysread more from the socket
195           itself.  If they read too much, the remaining data will be left in
196           this buffer.
197
198       $s->_rbuf_length
199           Returns the number of bytes in the read buffer.  This should always
200           be the same as:
201
202               length($s->_rbuf)
203
204           but might be more efficient.
205

SUBCLASSING

207       The read_response_headers() and read_entity_body() will invoke the
208       sysread() method when they need more data.  Subclasses might want to
209       override this method to control how reading takes place.
210
211       The object itself is a glob.  Subclasses should avoid using hash key
212       names prefixed with "http_" and "io_".
213

SEE ALSO

215       LWP, IO::Socket::INET, Net::HTTP::NB
216
218       Copyright 2001-2003 Gisle Aas.
219
220       This library is free software; you can redistribute it and/or modify it
221       under the same terms as Perl itself.
222
223
224
225perl v5.10.1                      2009-10-06                      Net::HTTP(3)
Impressum