lwp.html

来自「perl教程」· HTML 代码 · 共 615 行 · 第 1/3 页

HTML
615
字号
<p>The <strong>code</strong> is a numerical value that indicates the overall
outcome of the request.</p>
</li>
<li>
<p>The <strong>message</strong> is a short, human readable string that
corresponds to the <em>code</em>.</p>
</li>
<li>
<p>The <strong>headers</strong> contain additional information about the
response and describe the content.</p>
</li>
<li>
<p>The <strong>content</strong> is an arbitrary amount of data.</p>
</li>
</ul>
<p>Since we don't want to handle all possible <em>code</em> values directly in
our programs, a libwww-perl response object has methods that can be
used to query what kind of response this is.  The most commonly used
response classification methods are:</p>
<dl>
<dt><strong><a name="item_is_success"><code>is_success()</code></a></strong>

<dd>
<p>The request was was successfully received, understood or accepted.</p>
</dd>
</li>
<dt><strong><a name="item_is_error"><code>is_error()</code></a></strong>

<dd>
<p>The request failed.  The server or the resource might not be
available, access to the resource might be denied or other things might
have failed for some reason.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="the_user_agent">The User Agent</a></h2>
<p>Let us assume that we have created a <em>request</em> object. What do we
actually do with it in order to receive a <em>response</em>?</p>
<p>The answer is that you pass it to a <em>user agent</em> object and this
object takes care of all the things that need to be done
(like low-level communication and error handling) and returns
a <em>response</em> object. The user agent represents your
application on the network and provides you with an interface that
can accept <em>requests</em> and return <em>responses</em>.</p>
<p>The user agent is an interface layer between
your application code and the network.  Through this interface you are
able to access the various servers on the network.</p>
<p>The class name for the user agent is <code>LWP::UserAgent</code>.  Every
libwww-perl application that wants to communicate should create at
least one object of this class. The main method provided by this
object is request(). This method takes an <code>HTTP::Request</code> object as
argument and (eventually) returns a <code>HTTP::Response</code> object.</p>
<p>The user agent has many other attributes that let you
configure how it will interact with the network and with your
application.</p>
<ul>
<li>
<p>The <strong>timeout</strong> specifies how much time we give remote servers to
respond before the library disconnects and creates an
internal <em>timeout</em> response.</p>
</li>
<li>
<p>The <strong>agent</strong> specifies the name that your application should use when it
presents itself on the network.</p>
</li>
<li>
<p>The <strong>from</strong> attribute can be set to the e-mail address of the person
responsible for running the application.  If this is set, then the
address will be sent to the servers with every request.</p>
</li>
<li>
<p>The <strong>parse_head</strong> specifies whether we should initialize response
headers from the &lt;head&gt; section of HTML documents.</p>
</li>
<li>
<p>The <strong>proxy</strong> and <strong>no_proxy</strong> attributes specify if and when to go through
a proxy server. &lt;URL:http://www.w3.org/pub/WWW/Proxies/&gt;</p>
</li>
<li>
<p>The <strong>credentials</strong> provide a way to set up user names and
passwords needed to access certain services.</p>
</li>
</ul>
<p>Many applications want even more control over how they interact
with the network and they get this by sub-classing
<code>LWP::UserAgent</code>.  The library includes a
sub-class, <code>LWP::RobotUA</code>, for robot applications.</p>
<p>
</p>
<h2><a name="an_example">An Example</a></h2>
<p>This example shows how the user agent, a request and a response are
represented in actual perl code:</p>
<pre>
  <span class="comment"># Create a user agent object</span>
  <span class="keyword">use</span> <span class="variable">LWP::UserAgent</span><span class="operator">;</span>
  <span class="variable">$ua</span> <span class="operator">=</span> <span class="variable">LWP::UserAgent</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">;</span>
  <span class="variable">$ua</span><span class="operator">-&gt;</span><span class="variable">agent</span><span class="operator">(</span><span class="string">"MyApp/0.1 "</span><span class="operator">);</span>
</pre>
<pre>
  <span class="comment"># Create a request</span>
  <span class="keyword">my</span> <span class="variable">$req</span> <span class="operator">=</span> <span class="variable">HTTP::Request</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="string">POST</span> <span class="operator">=&gt;</span> <span class="string">'http://search.cpan.org/search'</span><span class="operator">);</span>
  <span class="variable">$req</span><span class="operator">-&gt;</span><span class="variable">content_type</span><span class="operator">(</span><span class="string">'application/x-www-form-urlencoded'</span><span class="operator">);</span>
  <span class="variable">$req</span><span class="operator">-&gt;</span><span class="variable">content</span><span class="operator">(</span><span class="string">'query=libwww-perl&amp;mode=dist'</span><span class="operator">);</span>
</pre>
<pre>
  <span class="comment"># Pass request to the user agent and get a response back</span>
  <span class="keyword">my</span> <span class="variable">$res</span> <span class="operator">=</span> <span class="variable">$ua</span><span class="operator">-&gt;</span><span class="variable">request</span><span class="operator">(</span><span class="variable">$req</span><span class="operator">);</span>
</pre>
<pre>
  <span class="comment"># Check the outcome of the response</span>
  <span class="keyword">if</span> <span class="operator">(</span><span class="variable">$res</span><span class="operator">-&gt;</span><span class="variable">is_success</span><span class="operator">)</span> <span class="operator">{</span>
      <span class="keyword">print</span> <span class="variable">$res</span><span class="operator">-&gt;</span><span class="variable">content</span><span class="operator">;</span>
  <span class="operator">}</span>
  <span class="keyword">else</span> <span class="operator">{</span>
      <span class="keyword">print</span> <span class="variable">$res</span><span class="operator">-&gt;</span><span class="variable">status_line</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
  <span class="operator">}</span>
</pre>
<p>The $ua is created once when the application starts up.  New request
objects should normally created for each request sent.</p>
<p>
</p>
<hr />
<h1><a name="network_support">NETWORK SUPPORT</a></h1>
<p>This section discusses the various protocol schemes and
the HTTP style methods that headers may be used for each.</p>
<p>For all requests, a &quot;User-Agent&quot; header is added and initialized from
the $ua-&gt;agent attribute before the request is handed to the network
layer.  In the same way, a &quot;From&quot; header is initialized from the
$ua-&gt;from attribute.</p>
<p>For all responses, the library adds a header called &quot;Client-Date&quot;.
This header holds the time when the response was received by
your application.  The format and semantics of the header are the
same as the server created &quot;Date&quot; header.  You may also encounter other
&quot;Client-XXX&quot; headers.  They are all generated by the library
internally and are not received from the servers.</p>
<p>
</p>
<h2><a name="http_requests">HTTP Requests</a></h2>
<p>HTTP requests are just handed off to an HTTP server and it
decides what happens.  Few servers implement methods beside the usual
&quot;GET&quot;, &quot;HEAD&quot;, &quot;POST&quot; and &quot;PUT&quot;, but CGI-scripts may implement
any method they like.</p>
<p>If the server is not available then the library will generate an
internal error response.</p>
<p>The library automatically adds a &quot;Host&quot; and a &quot;Content-Length&quot; header
to the HTTP request before it is sent over the network.</p>
<p>For a GET request you might want to add a &quot;If-Modified-Since&quot; or
&quot;If-None-Match&quot; header to make the request conditional.</p>
<p>For a POST request you should add the &quot;Content-Type&quot; header.  When you
try to emulate HTML &lt;FORM&gt; handling you should usually let the value
of the &quot;Content-Type&quot; header be &quot;application/x-www-form-urlencoded&quot;.
See <a href="../lib/lwpcook.html">the lwpcook manpage</a> for examples of this.</p>
<p>The libwww-perl HTTP implementation currently support the HTTP/1.1
and HTTP/1.0 protocol.</p>
<p>The library allows you to access proxy server through HTTP.  This
means that you can set up the library to forward all types of request
through the HTTP protocol module.  See <a href="../lib/LWP/UserAgent.html">the LWP::UserAgent manpage</a> for
documentation of this.</p>
<p>
</p>
<h2><a name="https_requests">HTTPS Requests</a></h2>
<p>HTTPS requests are HTTP requests over an encrypted network connection
using the SSL protocol developed by Netscape.  Everything about HTTP
requests above also apply to HTTPS requests.  In addition the library
will add the headers &quot;Client-SSL-Cipher&quot;, &quot;Client-SSL-Cert-Subject&quot; and
&quot;Client-SSL-Cert-Issuer&quot; to the response.  These headers denote the
encryption method used and the name of the server owner.</p>
<p>The request can contain the header &quot;If-SSL-Cert-Subject&quot; in order to
make the request conditional on the content of the server certificate.
If the certificate subject does not match, no request is sent to the
server and an internally generated error response is returned.  The
value of the &quot;If-SSL-Cert-Subject&quot; header is interpreted as a Perl
regular expression.</p>
<p>
</p>
<h2><a name="ftp_requests">FTP Requests</a></h2>
<p>The library currently supports GET, HEAD and PUT requests.  GET
retrieves a file or a directory listing from an FTP server.  PUT
stores a file on a ftp server.</p>
<p>You can specify a ftp account for servers that want this in addition
to user name and password.  This is specified by including an &quot;Account&quot;
header in the request.</p>
<p>User name/password can be specified using basic authorization or be
encoded in the URL.  Failed logins return an UNAUTHORIZED response with
&quot;WWW-Authenticate: Basic&quot; and can be treated like basic authorization
for HTTP.</p>
<p>The library supports ftp ASCII transfer mode by specifying the &quot;type=a&quot;
parameter in the URL. It also supports transfer of ranges for FTP transfers
using the &quot;Range&quot; header.</p>
<p>Directory listings are by default returned unprocessed (as returned
from the ftp server) with the content media type reported to be
&quot;text/ftp-dir-listing&quot;. The <code>File::Listing</code> module provides methods
for parsing of these directory listing.</p>
<p>The ftp module is also able to convert directory listings to HTML and
this can be requested via the standard HTTP content negotiation
mechanisms (add an &quot;Accept: text/html&quot; header in the request if you
want this).</p>
<p>For normal file retrievals, the &quot;Content-Type&quot; is guessed based on the
file name suffix. See <a href="../lib/LWP/MediaTypes.html">the LWP::MediaTypes manpage</a>.</p>
<p>The &quot;If-Modified-Since&quot; request header works for servers that implement
the MDTM command.  It will probably not work for directory listings though.</p>
<p>Example:</p>
<pre>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?