1Net::HTTP(3) User Contributed Perl Documentation Net::HTTP(3)
2
3
4
6 Net::HTTP - Low-level HTTP connection (client)
7
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
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. The list of encodings announced as accepted by this
75 client depends on availability of the following modules:
76 "Compress::Raw::Zlib" for deflate, and "IO::Compress::Gunzip" for
77 gzip.
78
79 $s->http_version
80 Get/set the HTTP version number that this client should announce.
81 This value can only be set to "1.0" or "1.1". The default is
82 "1.1".
83
84 $s->peer_http_version
85 Get/set the protocol version number of our peer. This value will
86 initially be "1.0", but will be updated by a successful
87 read_response_headers() method call.
88
89 $s->max_line_length
90 Get/set a limit on the length of response line and response header
91 lines. The default is 8192. A value of 0 means no limit.
92
93 $s->max_header_length
94 Get/set a limit on the number of header lines that a response can
95 have. The default is 128. A value of 0 means no limit.
96
97 $s->format_request($method, $uri, %headers, [$content])
98 Format a request message and return it as a string. If the headers
99 do not include a "Host" header, then a header is inserted with the
100 value of the "host" attribute. Headers like "Connection" and
101 "Keep-Alive" might also be added depending on the status of the
102 "keep_alive" attribute.
103
104 If $content is given (and it is non-empty), then a "Content-Length"
105 header is automatically added unless it was already present.
106
107 $s->write_request($method, $uri, %headers, [$content])
108 Format and send a request message. Arguments are the same as for
109 format_request(). Returns true if successful.
110
111 $s->format_chunk( $data )
112 Returns the string to be written for the given chunk of data.
113
114 $s->write_chunk($data)
115 Will write a new chunk of request entity body data. This method
116 should only be used if the "Transfer-Encoding" header with a value
117 of "chunked" was sent in the request. Note, writing zero-length
118 data is a no-op. Use the write_chunk_eof() method to signal end of
119 entity body data.
120
121 Returns true if successful.
122
123 $s->format_chunk_eof( %trailers )
124 Returns the string to be written for signaling EOF when a
125 "Transfer-Encoding" of "chunked" is used.
126
127 $s->write_chunk_eof( %trailers )
128 Will write eof marker for chunked data and optional trailers. Note
129 that trailers should not really be used unless is was signaled with
130 a "Trailer" header.
131
132 Returns true if successful.
133
134 ($code, $mess, %headers) = $s->read_response_headers( %opts )
135 Read response headers from server and return it. The $code is the
136 3 digit HTTP status code (see HTTP::Status) and $mess is the
137 textual message that came with it. Headers are then returned as
138 key/value pairs. Since key letter casing is not normalized and the
139 same key can even occur multiple times, assigning these values
140 directly to a hash is not wise. Only the $code is returned if this
141 method is called in scalar context.
142
143 As a side effect this method updates the 'peer_http_version'
144 attribute.
145
146 Options might be passed in as key/value pairs. There are currently
147 only two options supported; "laxed" and "junk_out".
148
149 The "laxed" option will make read_response_headers() more forgiving
150 towards servers that have not learned how to speak HTTP properly.
151 The "laxed" option is a boolean flag, and is enabled by passing in
152 a TRUE value. The "junk_out" option can be used to capture bad
153 header lines when "laxed" is enabled. The value should be an array
154 reference. Bad header lines will be pushed onto the array.
155
156 The "laxed" option must be specified in order to communicate with
157 pre-HTTP/1.0 servers that don't describe the response outcome or
158 the data they send back with a header block. For these servers
159 peer_http_version is set to "0.9" and this method returns (200,
160 "Assumed OK").
161
162 The method will raise an exception (die) if the server does not
163 speak proper HTTP or if the "max_line_length" or
164 "max_header_length" limits are reached. If the "laxed" option is
165 turned on and "max_line_length" and "max_header_length" checks are
166 turned off, then no exception will be raised and this method will
167 always return a response code.
168
169 $n = $s->read_entity_body($buf, $size);
170 Reads chunks of the entity body content. Basically the same
171 interface as for read() and sysread(), but the buffer offset
172 argument is not supported yet. This method should only be called
173 after a successful read_response_headers() call.
174
175 The return value will be "undef" on read errors, 0 on EOF, -1 if no
176 data could be returned this time, otherwise the number of bytes
177 assigned to $buf. The $buf is set to "" when the return value is
178 -1.
179
180 You normally want to retry this call if this function returns
181 either -1 or "undef" with $! as EINTR or EAGAIN (see Errno). EINTR
182 can happen if the application catches signals and EAGAIN can happen
183 if you made the socket non-blocking.
184
185 This method will raise exceptions (die) if the server does not
186 speak proper HTTP. This can only happen when reading chunked data.
187
188 %headers = $s->get_trailers
189 After read_entity_body() has returned 0 to indicate end of the
190 entity body, you might call this method to pick up any trailers.
191
192 $s->_rbuf
193 Get/set the read buffer content. The read_response_headers() and
194 read_entity_body() methods use an internal buffer which they will
195 look for data before they actually sysread more from the socket
196 itself. If they read too much, the remaining data will be left in
197 this buffer.
198
199 $s->_rbuf_length
200 Returns the number of bytes in the read buffer. This should always
201 be the same as:
202
203 length($s->_rbuf)
204
205 but might be more efficient.
206
208 The read_response_headers() and read_entity_body() will invoke the
209 sysread() method when they need more data. Subclasses might want to
210 override this method to control how reading takes place.
211
212 The object itself is a glob. Subclasses should avoid using hash key
213 names prefixed with "http_" and "io_".
214
216 LWP, IO::Socket::INET, Net::HTTP::NB
217
219 Copyright 2001-2003 Gisle Aas.
220
221 This library is free software; you can redistribute it and/or modify it
222 under the same terms as Perl itself.
223
224
225
226perl v5.12.4 2009-11-21 Net::HTTP(3)