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 support
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 "http_ver‐
69 sion" 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 accept 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 4096. A value of 0 means no limit.
91
92 $s->max_header_length
93 Get/set a limit on the number of headers 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 "Trans‐
124 fer-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 tex‐
136 tual 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 inter‐
170 face as for read() and sysread(), but the buffer offset argument is
171 not supported yet. This method should only be called after a suc‐
172 cessful 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 set to "" when the return value is -1.
177
178 This method will raise exceptions (die) if the server does not
179 speak proper HTTP. This can only happen when reading chunked data.
180
181 %headers = $s->get_trailers
182 After read_entity_body() has returned 0 to indicate end of the
183 entity body, you might call this method to pick up any trailers.
184
185 $s->_rbuf
186 Get/set the read buffer content. The read_response_headers() and
187 read_entity_body() methods use an internal buffer which they will
188 look for data before they actually sysread more from the socket
189 itself. If they read too much, the remaining data will be left in
190 this buffer.
191
192 $s->_rbuf_length
193 Returns the number of bytes in the read buffer. This should always
194 be the same as:
195
196 length($s->_rbuf)
197
198 but might be more efficient.
199
201 The read_response_headers() and read_entity_body() will invoke the sys‐
202 read() method when they need more data. Subclasses might want to over‐
203 ride this method to control how reading takes place.
204
205 The object itself is a glob. Subclasses should avoid using hash key
206 names prefixed with "http_" and "io_".
207
209 LWP, IO::Socket::INET, Net::HTTP::NB
210
212 Copyright 2001-2003 Gisle Aas.
213
214 This library is free software; you can redistribute it and/or modify it
215 under the same terms as Perl itself.
216
217
218
219perl v5.8.8 2004-04-06 Net::HTTP(3)