📄 http.html
字号:
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>Net::HTTP - Low-level HTTP connection</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>Net::HTTP - Low-level HTTP connection</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#synopsis">SYNOPSIS</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<li><a href="#subclassing">SUBCLASSING</a></li>
<li><a href="#see_also">SEE ALSO</a></li>
<li><a href="#copyright">COPYRIGHT</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Net::HTTP - Low-level HTTP connection (client)</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
<span class="keyword">use</span> <span class="variable">Net::HTTP</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$s</span> <span class="operator">=</span> <span class="variable">Net::HTTP</span><span class="operator">-></span><span class="variable">new</span><span class="operator">(</span><span class="string">Host</span> <span class="operator">=></span> <span class="string">"www.perl.com"</span><span class="operator">)</span> <span class="operator">||</span> <span class="keyword">die</span> <span class="variable">$@</span><span class="operator">;</span>
<span class="variable">$s</span><span class="operator">-></span><span class="variable">write_request</span><span class="operator">(</span><span class="string">GET</span> <span class="operator">=></span> <span class="string">"/"</span><span class="operator">,</span> <span class="string">'User-Agent'</span> <span class="operator">=></span> <span class="string">"Mozilla/5.0"</span><span class="operator">);</span>
<span class="keyword">my</span><span class="operator">(</span><span class="variable">$code</span><span class="operator">,</span> <span class="variable">$mess</span><span class="operator">,</span> <span class="variable">%h</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">$s</span><span class="operator">-></span><span class="variable">read_response_headers</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">while</span> <span class="operator">(</span><span class="number">1</span><span class="operator">)</span> <span class="operator">{</span>
<span class="keyword">my</span> <span class="variable">$buf</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$n</span> <span class="operator">=</span> <span class="variable">$s</span><span class="operator">-></span><span class="variable">read_entity_body</span><span class="operator">(</span><span class="variable">$buf</span><span class="operator">,</span> <span class="number">1024</span><span class="operator">);</span>
<span class="keyword">die</span> <span class="string">"read failed: $!"</span> <span class="keyword">unless</span> <span class="keyword">defined</span> <span class="variable">$n</span><span class="operator">;</span>
<span class="keyword">last</span> <span class="keyword">unless</span> <span class="variable">$n</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="variable">$buf</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>The <code>Net::HTTP</code> class is a low-level HTTP client. An instance of the
<code>Net::HTTP</code> class represents a connection to an HTTP server. The
HTTP protocol is described in RFC 2616. The <code>Net::HTTP</code> class
support <code>HTTP/1.0</code> and <code>HTTP/1.1</code>.</p>
<p><code>Net::HTTP</code> is a sub-class of <code>IO::Socket::INET</code>. 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.</p>
<p>The following methods are provided (in addition to those of
<code>IO::Socket::INET</code>):</p>
<dl>
<dt><strong><a name="item_new">$s = Net::HTTP->new( %options )</a></strong>
<dd>
<p>The <code>Net::HTTP</code> constructor method takes the same options as
<code>IO::Socket::INET</code>'s as well as these:</p>
</dd>
<dd>
<pre>
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</pre>
</dd>
<dd>
<p>The <code>Host</code> option is also the default for <code>IO::Socket::INET</code>'s
<code>PeerAddr</code>. The <code>PeerPort</code> defaults to 80 if not provided.</p>
</dd>
<dd>
<p>The <code>Listen</code> option provided by <code>IO::Socket::INET</code>'s constructor
method is not allowed.</p>
</dd>
<dd>
<p>If unable to connect to the given HTTP server then the constructor
returns <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> and $@ contains the reason. After a successful
connect, a <code>Net:HTTP</code> object is returned.</p>
</dd>
</li>
<dt><strong><a name="item_host">$s->host</a></strong>
<dd>
<p>Get/set the default value of the <code>Host</code> header to send. The $host
must not be set to an empty string (or <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>) for HTTP/1.1.</p>
</dd>
</li>
<dt><strong><a name="item_keep_alive">$s->keep_alive</a></strong>
<dd>
<p>Get/set the <em>keep-alive</em> value. If this value is TRUE then the
request will be sent with headers indicating that the server should try
to keep the connection open so that multiple requests can be sent.</p>
</dd>
<dd>
<p>The actual headers set will depend on the value of the <a href="#item_http_version"><code>http_version</code></a>
and <a href="#item_peer_http_version"><code>peer_http_version</code></a> attributes.</p>
</dd>
</li>
<dt><strong><a name="item_send_te">$s->send_te</a></strong>
<dd>
<p>Get/set the a value indicating if the request will be sent with a "TE"
header to indicate the transfer encodings that the server can choose to
use. If the <code>Compress::Zlib</code> module is installed then this will
announce that this client accept both the <em>deflate</em> and <em>gzip</em>
encodings.</p>
</dd>
</li>
<dt><strong><a name="item_http_version">$s->http_version</a></strong>
<dd>
<p>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".</p>
</dd>
</li>
<dt><strong><a name="item_peer_http_version">$s->peer_http_version</a></strong>
<dd>
<p>Get/set the protocol version number of our peer. This value will
initially be "1.0", but will be updated by a successful
<a href="#item_read_response_headers"><code>read_response_headers()</code></a> method call.</p>
</dd>
</li>
<dt><strong><a name="item_max_line_length">$s->max_line_length</a></strong>
<dd>
<p>Get/set a limit on the length of response line and response header
lines. The default is 4096. A value of 0 means no limit.</p>
</dd>
</li>
<dt><strong><a name="item_max_header_length">$s->max_header_length</a></strong>
<dd>
<p>Get/set a limit on the number of headers lines that a response can
have. The default is 128. A value of 0 means no limit.</p>
</dd>
</li>
<dt><strong><a name="item_format_request">$s->format_request($method, $uri, %headers, [$content])</a></strong>
<dd>
<p>Format a request message and return it as a string. If the headers do
not include a <code>Host</code> header, then a header is inserted with the value
of the <a href="#item_host"><code>host</code></a> attribute. Headers like <code>Connection</code> and
<code>Keep-Alive</code> might also be added depending on the status of the
<a href="#item_keep_alive"><code>keep_alive</code></a> attribute.</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -