useragent.html
来自「perl教程」· HTML 代码 · 共 735 行 · 第 1/3 页
HTML
735 行
<?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>LWP::UserAgent - Web user agent class</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>LWP::UserAgent - Web user agent class</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="#constructor_methods">CONSTRUCTOR METHODS</a></li>
<li><a href="#attributes">ATTRIBUTES</a></li>
<ul>
<li><a href="#proxy_attributes">Proxy attributes</a></li>
</ul>
<li><a href="#request_methods">REQUEST METHODS</a></li>
<ul>
<li><a href="#callback_methods">Callback methods</a></li>
</ul>
<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>LWP::UserAgent - Web user agent class</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
<span class="keyword">require</span> <span class="variable">LWP::UserAgent</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$ua</span> <span class="operator">=</span> <span class="variable">LWP::UserAgent</span><span class="operator">-></span><span class="variable">new</span><span class="operator">;</span>
<span class="variable">$ua</span><span class="operator">-></span><span class="variable">timeout</span><span class="operator">(</span><span class="number">10</span><span class="operator">);</span>
<span class="variable">$ua</span><span class="operator">-></span><span class="variable">env_proxy</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$response</span> <span class="operator">=</span> <span class="variable">$ua</span><span class="operator">-></span><span class="variable">get</span><span class="operator">(</span><span class="string">'http://search.cpan.org/'</span><span class="operator">);</span>
<span class="keyword">if</span> <span class="operator">(</span><span class="variable">$response</span><span class="operator">-></span><span class="variable">is_success</span><span class="operator">)</span> <span class="operator">{</span>
<span class="keyword">print</span> <span class="variable">$response</span><span class="operator">-></span><span class="variable">content</span><span class="operator">;</span> <span class="comment"># or whatever</span>
<span class="operator">}</span>
<span class="keyword">else</span> <span class="operator">{</span>
<span class="keyword">die</span> <span class="variable">$response</span><span class="operator">-></span><span class="variable">status_line</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>The <code>LWP::UserAgent</code> is a class implementing a web user agent.
<code>LWP::UserAgent</code> objects can be used to dispatch web requests.</p>
<p>In normal use the application creates an <code>LWP::UserAgent</code> object, and
then configures it with values for timeouts, proxies, name, etc. It
then creates an instance of <code>HTTP::Request</code> for the request that
needs to be performed. This request is then passed to one of the
request method the UserAgent, which dispatches it using the relevant
protocol, and returns a <code>HTTP::Response</code> object. There are
convenience methods for sending the most common request types: get(),
<a href="#item_head"><code>head()</code></a> and post(). When using these methods then the creation of the
request object is hidden as shown in the synopsis above.</p>
<p>The basic approach of the library is to use HTTP style communication
for all protocol schemes. This means that you will construct
<code>HTTP::Request</code> objects and receive <code>HTTP::Response</code> objects even
for non-HTTP resources like <em>gopher</em> and <em>ftp</em>. In order to achieve
even more similarity to HTTP style communications, gopher menus and
file directories are converted to HTML documents.</p>
<p>
</p>
<hr />
<h1><a name="constructor_methods">CONSTRUCTOR METHODS</a></h1>
<p>The following constructor methods are available:</p>
<dl>
<dt><strong><a name="item_new">$ua = LWP::UserAgent->new( %options )</a></strong>
<dd>
<p>This method constructs a new <code>LWP::UserAgent</code> object and returns it.
Key/value pair arguments may be provided to set up the initial state.
The following options correspond to attribute methods described below:</p>
</dd>
<dd>
<pre>
KEY DEFAULT
----------- --------------------
agent "libwww-perl/#.##"
from undef
conn_cache undef
cookie_jar undef
default_headers HTTP::Headers->new
max_size undef
max_redirect 7
parse_head 1
protocols_allowed undef
protocols_forbidden undef
requests_redirectable ['GET', 'HEAD']
timeout 180</pre>
</dd>
<dd>
<p>The following additional options are also accepted: If the
<a href="#item_env_proxy"><code>env_proxy</code></a> option is passed in with a TRUE value, then proxy
settings are read from environment variables (see <a href="#item_env_proxy"><code>env_proxy()</code></a> method
below). If the <code>keep_alive</code> option is passed in, then a
<code>LWP::ConnCache</code> is set up (see <a href="#item_conn_cache"><code>conn_cache()</code></a> method below). The
<code>keep_alive</code> value is passed on as the <code>total_capacity</code> for the
connection cache.</p>
</dd>
</li>
<dt><strong><a name="item_clone">$ua->clone</a></strong>
<dd>
<p>Returns a copy of the LWP::UserAgent object.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="attributes">ATTRIBUTES</a></h1>
<p>The settings of the configuration attributes modify the behaviour of the
<code>LWP::UserAgent</code> when it dispatches requests. Most of these can also
be initialized by options passed to the constructor method.</p>
<p>The following attributes methods are provided. The attribute value is
left unchanged if no argument is given. The return value from each
method is the old attribute value.</p>
<dl>
<dt><strong><a name="item_agent">$ua->agent</a></strong>
<dt><strong>$ua->agent( $product_id )</strong>
<dd>
<p>Get/set the product token that is used to identify the user agent on
the network. The agent value is sent as the "User-Agent" header in
the requests. The default is the string returned by the <a href="#item__agent"><code>_agent()</code></a>
method (see below).</p>
</dd>
<dd>
<p>If the $product_id ends with space then the <a href="#item__agent"><code>_agent()</code></a> string is
appended to it.</p>
</dd>
<dd>
<p>The user agent string should be one or more simple product identifiers
with an optional version number separated by the "/" character.
Examples are:</p>
</dd>
<dd>
<pre>
<span class="variable">$ua</span><span class="operator">-></span><span class="variable">agent</span><span class="operator">(</span><span class="string">'Checkbot/0.4 '</span> <span class="operator">.</span> <span class="variable">$ua</span><span class="operator">-></span><span class="variable">_agent</span><span class="operator">);</span>
<span class="variable">$ua</span><span class="operator">-></span><span class="variable">agent</span><span class="operator">(</span><span class="string">'Checkbot/0.4 '</span><span class="operator">);</span> <span class="comment"># same as above</span>
<span class="variable">$ua</span><span class="operator">-></span><span class="variable">agent</span><span class="operator">(</span><span class="string">'Mozilla/5.0'</span><span class="operator">);</span>
<span class="variable">$ua</span><span class="operator">-></span><span class="variable">agent</span><span class="operator">(</span><span class="string">""</span><span class="operator">);</span> <span class="comment"># don't identify</span>
</pre>
</dd>
</li>
<dt><strong><a name="item__agent">$ua->_agent</a></strong>
<dd>
<p>Returns the default agent identifier. This is a string of the form
"libwww-perl/#.##", where "#.##" is substituted with the version number
of this library.</p>
</dd>
</li>
<dt><strong><a name="item_from">$ua->from</a></strong>
<dt><strong>$ua->from( $email_address )</strong>
<dd>
<p>Get/set the e-mail address for the human user who controls
the requesting user agent. The address should be machine-usable, as
defined in RFC 822. The <a href="#item_from"><code>from</code></a> value is send as the "From" header in
the requests. Example:</p>
</dd>
<dd>
<pre>
<span class="variable">$ua</span><span class="operator">-></span><span class="variable">from</span><span class="operator">(</span><span class="string">'gaas@cpan.org'</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>The default is to not send a "From" header. See the <a href="#item_default_headers"><code>default_headers()</code></a>
method for the more general interface that allow any header to be defaulted.</p>
</dd>
</li>
<dt><strong><a name="item_cookie_jar">$ua->cookie_jar</a></strong>
<dt><strong>$ua->cookie_jar( $cookie_jar_obj )</strong>
<dd>
<p>Get/set the cookie jar object to use. The only requirement is that
the cookie jar object must implement the <code>extract_cookies($request)</code> and
<code>add_cookie_header($response)</code> methods. These methods will then be
invoked by the user agent as requests are sent and responses are
received. Normally this will be a <code>HTTP::Cookies</code> object or some
subclass.</p>
</dd>
<dd>
<p>The default is to have no cookie_jar, i.e. never automatically add
"Cookie" headers to the requests.</p>
</dd>
<dd>
<p>Shortcut: If a reference to a plain hash is passed in as the
$cookie_jar_object, then it is replaced with an instance of
<code>HTTP::Cookies</code> that is initialized based on the hash. This form also
automatically loads the <code>HTTP::Cookies</code> module. It means that:</p>
</dd>
<dd>
<pre>
<span class="variable">$ua</span><span class="operator">-></span><span class="variable">cookie_jar</span><span class="operator">(</span><span class="operator">{</span> <span class="string">file</span> <span class="operator">=></span> <span class="string">"$ENV{HOME}/.cookies.txt"</span> <span class="operator">}</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>is really just a shortcut for:</p>
</dd>
<dd>
<pre>
<span class="keyword">require</span> <span class="variable">HTTP::Cookies</span><span class="operator">;</span>
<span class="variable">$ua</span><span class="operator">-></span><span class="variable">cookie_jar</span><span class="operator">(</span><span class="variable">HTTP::Cookies</span><span class="operator">-></span><span class="variable">new</span><span class="operator">(</span><span class="string">file</span> <span class="operator">=></span> <span class="string">"$ENV{HOME}/.cookies.txt"</span><span class="operator">));</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_default_headers">$ua->default_headers</a></strong>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?