useragent.html
来自「perl教程」· HTML 代码 · 共 735 行 · 第 1/3 页
HTML
735 行
</dd>
<dd>
<p>Fields names that start with ":" are special. These will not
initialize headers of the request but will determine how the response
content is treated. The following special field names are recognized:</p>
</dd>
<dd>
<pre>
:content_file => $filename
:content_cb => \&callback
:read_size_hint => $bytes</pre>
</dd>
<dd>
<p>If a $filename is provided with the <code>:content_file</code> option, then the
response content will be saved here instead of in the response
object. If a callback is provided with the <code>:content_cb</code> option then
this function will be called for each chunk of the response content as
it is received from the server. If neither of these options are
given, then the response content will accumulate in the response
object itself. This might not be suitable for very large response
bodies. Only one of <code>:content_file</code> or <code>:content_cb</code> can be
specified. The content of unsuccessful responses will always
accumulate in the response object itself, regardless of the
<code>:content_file</code> or <code>:content_cb</code> options passed in.</p>
</dd>
<dd>
<p>The <code>:read_size_hint</code> option is passed to the protocol module which
will try to read data from the server in chunks of this size. A
smaller value for the <code>:read_size_hint</code> will result in a higher
number of callback invocations.</p>
</dd>
<dd>
<p>The callback function is called with 3 arguments: a chunk of data, a
reference to the response object, and a reference to the protocol
object. The callback can abort the request by invoking die(). The
exception message will show up as the "X-Died" header field in the
response returned by the <a href="#item_get"><code>get()</code></a> function.</p>
</dd>
</li>
<dt><strong><a name="item_head">$ua->head( $url )</a></strong>
<dt><strong>$ua->head( $url , $field_name => $value, ... )</strong>
<dd>
<p>This method will dispatch a <code>HEAD</code> request on the given $url.
Otherwise it works like the <a href="#item_get"><code>get()</code></a> method described above.</p>
</dd>
</li>
<dt><strong><a name="item_post">$ua->post( $url, \%form )</a></strong>
<dt><strong>$ua->post( $url, \@form )</strong>
<dt><strong>$ua->post( $url, \%form, $field_name => $value, ... )</strong>
<dd>
<p>This method will dispatch a <code>POST</code> request on the given $url, with
%form or @form providing the key/value pairs for the fill-in form
content. Additional headers and content options are the same as for
the <a href="#item_get"><code>get()</code></a> method.</p>
</dd>
<dd>
<p>This method will use the <code>POST()</code> function from <code>HTTP::Request::Common</code>
to build the request. See <a href="../../lib/HTTP/Request/Common.html">the HTTP::Request::Common manpage</a> for a details on
how to pass form content and other advanced features.</p>
</dd>
</li>
<dt><strong><a name="item_mirror">$ua->mirror( $url, $filename )</a></strong>
<dd>
<p>This method will get the document identified by $url and store it in
file called $filename. If the file already exists, then the request
will contain an "If-Modified-Since" header matching the modification
time of the file. If the document on the server has not changed since
this time, then nothing happens. If the document has been updated, it
will be downloaded again. The modification time of the file will be
forced to match that of the server.</p>
</dd>
<dd>
<p>The return value is the the response object.</p>
</dd>
</li>
<dt><strong><a name="item_request">$ua->request( $request )</a></strong>
<dt><strong>$ua->request( $request, $content_file )</strong>
<dt><strong>$ua->request( $request, $content_cb )</strong>
<dt><strong>$ua->request( $request, $content_cb, $read_size_hint )</strong>
<dd>
<p>This method will dispatch the given $request object. Normally this
will be an instance of the <code>HTTP::Request</code> class, but any object with
a similar interface will do. The return value is a response object.
See <a href="../../lib/HTTP/Request.html">the HTTP::Request manpage</a> and <a href="../../lib/HTTP/Response.html">the HTTP::Response manpage</a> for a description of the
interface provided by these classes.</p>
</dd>
<dd>
<p>The <a href="#item_request"><code>request()</code></a> method will process redirects and authentication
responses transparently. This means that it may actually send several
simple requests via the <a href="#item_simple_request"><code>simple_request()</code></a> method described below.</p>
</dd>
<dd>
<p>The request methods described above; get(), head(), <a href="#item_post"><code>post()</code></a> and
mirror(), will all dispatch the request they build via this method.
They are convenience methods that simply hides the creation of the
request object for you.</p>
</dd>
<dd>
<p>The $content_file, $content_cb and $read_size_hint all correspond to
options described with the <a href="#item_get"><code>get()</code></a> method above.</p>
</dd>
<dd>
<p>You are allowed to use a CODE reference as <code>content</code> in the request
object passed in. The <code>content</code> function should return the content
when called. The content can be returned in chunks. The content
function will be invoked repeatedly until it return an empty string to
signal that there is no more content.</p>
</dd>
</li>
<dt><strong><a name="item_simple_request">$ua->simple_request( $request )</a></strong>
<dt><strong>$ua->simple_request( $request, $content_file )</strong>
<dt><strong>$ua->simple_request( $request, $content_cb )</strong>
<dt><strong>$ua->simple_request( $request, $content_cb, $read_size_hint )</strong>
<dd>
<p>This method dispatches a single request and returns the response
received. Arguments are the same as for <a href="#item_request"><code>request()</code></a> described above.</p>
</dd>
<dd>
<p>The difference from <a href="#item_request"><code>request()</code></a> is that <a href="#item_simple_request"><code>simple_request()</code></a> will not try to
handle redirects or authentication responses. The <a href="#item_request"><code>request()</code></a> method
will in fact invoke this method for each simple request it sends.</p>
</dd>
</li>
<dt><strong><a name="item_is_protocol_supported">$ua->is_protocol_supported( $scheme )</a></strong>
<dd>
<p>You can use this method to test whether this user agent object supports the
specified <code>scheme</code>. (The <code>scheme</code> might be a string (like 'http' or
'ftp') or it might be an URI object reference.)</p>
</dd>
<dd>
<p>Whether a scheme is supported, is determined by the user agent's
<a href="#item_protocols_allowed"><code>protocols_allowed</code></a> or <a href="#item_protocols_forbidden"><code>protocols_forbidden</code></a> lists (if any), and by
the capabilities of LWP. I.e., this will return TRUE only if LWP
supports this protocol <em>and</em> it's permitted for this particular
object.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="callback_methods">Callback methods</a></h2>
<p>The following methods will be invoked as requests are processed. These
methods are documented here because subclasses of <code>LWP::UserAgent</code>
might want to override their behaviour.</p>
<dl>
<dt><strong><a name="item_prepare_request">$ua->prepare_request( $request )</a></strong>
<dd>
<p>This method is invoked by simple_request(). Its task is to modify the
given $request object by setting up various headers based on the
attributes of the user agent. The return value should normally be the
$request object passed in. If a different request object is returned
it will be the one actually processed.</p>
</dd>
<dd>
<p>The headers affected by the base implementation are; "User-Agent",
"From", "Range" and "Cookie".</p>
</dd>
</li>
<dt><strong><a name="item_redirect_ok">$ua->redirect_ok( $prospective_request, $response )</a></strong>
<dd>
<p>This method is called by <a href="#item_request"><code>request()</code></a> before it tries to follow a
redirection to the request in $response. This should return a TRUE
value if this redirection is permissible. The $prospective_request
will be the request to be sent if this method returns TRUE.</p>
</dd>
<dd>
<p>The base implementation will return FALSE unless the method
is in the object's <a href="#item_requests_redirectable"><code>requests_redirectable</code></a> list,
FALSE if the proposed redirection is to a "file://..."
URL, and TRUE otherwise.</p>
</dd>
</li>
<dt><strong><a name="item_get_basic_credentials">$ua->get_basic_credentials( $realm, $uri, $isproxy )</a></strong>
<dd>
<p>This is called by <a href="#item_request"><code>request()</code></a> to retrieve credentials for documents
protected by Basic or Digest Authentication. The arguments passed in
is the $realm provided by the server, the $uri requested and a boolean
flag to indicate if this is authentication against a proxy server.</p>
</dd>
<dd>
<p>The method should return a username and password. It should return an
empty list to abort the authentication resolution attempt. Subclasses
can override this method to prompt the user for the information. An
example of this can be found in <code>lwp-request</code> program distributed
with this library.</p>
</dd>
<dd>
<p>The base implementation simply checks a set of pre-stored member
variables, set up with the <a href="#item_credentials"><code>credentials()</code></a> method.</p>
</dd>
</li>
<dt><strong><a name="item_progress">$ua->progress( $status, $response )</a></strong>
<dd>
<p>This is called frequently as the response is received regardless of
how the content is processed. The method is called with $status
"begin" at the start of processing the request and with $state "end"
before the request method returns. In between these $status will be
the fraction of the response currently received or the string "tick"
if the fraction can't be calculated.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p>See <a href="../../lib/LWP.html">the LWP manpage</a> for a complete overview of libwww-perl5. See <a href="../../lib/lwpcook.html">the lwpcook manpage</a>
and the scripts <em>lwp-request</em> and <em>lwp-download</em> for examples of
usage.</p>
<p>See <a href="../../lib/HTTP/Request.html">the HTTP::Request manpage</a> and <a href="../../lib/HTTP/Response.html">the HTTP::Response manpage</a> for a description of the
message objects dispatched and received. See <a href="../../lib/HTTP/Request/Common.html">the HTTP::Request::Common manpage</a>
and <a href="../../lib/HTML/Form.html">the HTML::Form manpage</a> for other ways to build request objects.</p>
<p>See <a href="../../WWW/Mechanize.html">the WWW::Mechanize manpage</a> and <a href="../../WWW/Search.html">the WWW::Search manpage</a> for examples of more
specialized user agents based on <code>LWP::UserAgent</code>.</p>
<p>
</p>
<hr />
<h1><a name="copyright">COPYRIGHT</a></h1>
<p>Copyright 1995-2004 Gisle Aas.</p>
<p>This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.</p>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?