useragent.html
来自「perl教程」· HTML 代码 · 共 735 行 · 第 1/3 页
HTML
735 行
<dt><strong>$ua->default_headers( $headers_obj )</strong>
<dd>
<p>Get/set the headers object that will provide default header values for
any requests sent. By default this will be an empty <code>HTTP::Headers</code>
object. Example:</p>
</dd>
<dd>
<pre>
<span class="variable">$ua</span><span class="operator">-></span><span class="variable">default_headers</span><span class="operator">-></span><span class="variable">push_header</span><span class="operator">(</span><span class="string">'Accept-Language'</span> <span class="operator">=></span> <span class="string">"no, en"</span><span class="operator">);</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_default_header">$ua->default_header( $field )</a></strong>
<dt><strong>$ua->default_header( $field => $value )</strong>
<dd>
<p>This is just a short-cut for $ua->default_headers->header( $field =>
$value ). Example:</p>
</dd>
<dd>
<pre>
<span class="variable">$ua</span><span class="operator">-></span><span class="variable">default_header</span><span class="operator">(</span><span class="string">'Accept-Language'</span> <span class="operator">=></span> <span class="string">"no, en"</span><span class="operator">);</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_conn_cache">$ua->conn_cache</a></strong>
<dt><strong>$ua->conn_cache( $cache_obj )</strong>
<dd>
<p>Get/set the <code>LWP::ConnCache</code> object to use. See <a href="../../lib/LWP/ConnCache.html">the LWP::ConnCache manpage</a>
for details.</p>
</dd>
</li>
<dt><strong><a name="item_credentials">$ua->credentials( $netloc, $realm, $uname, $pass )</a></strong>
<dd>
<p>Set the user name and password to be used for a realm. It is often more
useful to specialize the <a href="#item_get_basic_credentials"><code>get_basic_credentials()</code></a> method instead.</p>
</dd>
</li>
<dt><strong><a name="item_max_size">$ua->max_size</a></strong>
<dt><strong>$ua->max_size( $bytes )</strong>
<dd>
<p>Get/set the size limit for response content. The default is <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>,
which means that there is no limit. If the returned response content
is only partial, because the size limit was exceeded, then a
"Client-Aborted" header will be added to the response. The content
might end up longer than <a href="#item_max_size"><code>max_size</code></a> as we abort once appending a
chunk of data makes the length exceed the limit. The "Content-Length"
header, if present, will indicate the length of the full content and
will normally not be the same as <a href="../../lib/Pod/perlfunc.html#item_length"><code>length($res->content)</code></a>.</p>
</dd>
</li>
<dt><strong><a name="item_max_redirect">$ua->max_redirect</a></strong>
<dt><strong>$ua->max_redirect( $n )</strong>
<dd>
<p>This reads or sets the object's limit of how many times it will obey
redirection responses in a given request cycle.</p>
</dd>
<dd>
<p>By default, the value is 7. This means that if you call <a href="#item_request"><code>request()</code></a>
method and the response is a redirect elsewhere which is in turn a
redirect, and so on seven times, then LWP gives up after that seventh
request.</p>
</dd>
</li>
<dt><strong><a name="item_parse_head">$ua->parse_head</a></strong>
<dt><strong>$ua->parse_head( $boolean )</strong>
<dd>
<p>Get/set a value indicating whether we should initialize response
headers from the <head> section of HTML documents. The default is
TRUE. Do not turn this off, unless you know what you are doing.</p>
</dd>
</li>
<dt><strong><a name="item_protocols_allowed">$ua->protocols_allowed</a></strong>
<dt><strong>$ua->protocols_allowed( \@protocols )</strong>
<dd>
<p>This reads (or sets) this user agent's list of protocols that the
request methods will exclusively allow. The protocol names are case
insensitive.</p>
</dd>
<dd>
<p>For example: <a href="#item_protocols_allowed"><code>$ua->protocols_allowed( [ 'http', 'https'] );</code></a>
means that this user agent will <em>allow only</em> those protocols,
and attempts to use this user agent to access URLs with any other
schemes (like "ftp://...") will result in a 500 error.</p>
</dd>
<dd>
<p>To delete the list, call: <a href="#item_protocols_allowed"><code>$ua->protocols_allowed(undef)</code></a></p>
</dd>
<dd>
<p>By default, an object has neither a <a href="#item_protocols_allowed"><code>protocols_allowed</code></a> list, nor a
<a href="#item_protocols_forbidden"><code>protocols_forbidden</code></a> list.</p>
</dd>
<dd>
<p>Note that having a <a href="#item_protocols_allowed"><code>protocols_allowed</code></a> list causes any
<a href="#item_protocols_forbidden"><code>protocols_forbidden</code></a> list to be ignored.</p>
</dd>
</li>
<dt><strong><a name="item_protocols_forbidden">$ua->protocols_forbidden</a></strong>
<dt><strong>$ua->protocols_forbidden( \@protocols )</strong>
<dd>
<p>This reads (or sets) this user agent's list of protocols that the
request method will <em>not</em> allow. The protocol names are case
insensitive.</p>
</dd>
<dd>
<p>For example: <a href="#item_protocols_forbidden"><code>$ua->protocols_forbidden( [ 'file', 'mailto'] );</code></a>
means that this user agent will <em>not</em> allow those protocols, and
attempts to use this user agent to access URLs with those schemes
will result in a 500 error.</p>
</dd>
<dd>
<p>To delete the list, call: <a href="#item_protocols_forbidden"><code>$ua->protocols_forbidden(undef)</code></a></p>
</dd>
</li>
<dt><strong><a name="item_requests_redirectable">$ua->requests_redirectable</a></strong>
<dt><strong>$ua->requests_redirectable( \@requests )</strong>
<dd>
<p>This reads or sets the object's list of request names that
<a href="#item_redirect_ok"><code>$ua->redirect_ok(...)</code></a> will allow redirection for. By
default, this is <code>['GET', 'HEAD']</code>, as per RFC 2616. To
change to include 'POST', consider:</p>
</dd>
<dd>
<pre>
<span class="keyword">push</span> <span class="variable">@</span><span class="operator">{</span> <span class="variable">$ua</span><span class="operator">-></span><span class="variable">requests_redirectable</span> <span class="operator">}</span><span class="operator">,</span> <span class="string">'POST'</span><span class="operator">;</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_timeout">$ua->timeout</a></strong>
<dt><strong>$ua->timeout( $secs )</strong>
<dd>
<p>Get/set the timeout value in seconds. The default <a href="#item_timeout"><code>timeout()</code></a> value is
180 seconds, i.e. 3 minutes.</p>
</dd>
<dd>
<p>The requests is aborted if no activity on the connection to the server
is observed for <a href="#item_timeout"><code>timeout</code></a> seconds. This means that the time it takes
for the complete transaction and the <a href="#item_request"><code>request()</code></a> method to actually
return might be longer.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="proxy_attributes">Proxy attributes</a></h2>
<p>The following methods set up when requests should be passed via a
proxy server.</p>
<dl>
<dt><strong><a name="item_proxy">$ua->proxy(\@schemes, $proxy_url)</a></strong>
<dt><strong>$ua->proxy($scheme, $proxy_url)</strong>
<dd>
<p>Set/retrieve proxy URL for a scheme:</p>
</dd>
<dd>
<pre>
<span class="variable">$ua</span><span class="operator">-></span><span class="variable">proxy</span><span class="operator">(</span><span class="operator">[</span><span class="string">'http'</span><span class="operator">,</span> <span class="string">'ftp'</span><span class="operator">]</span><span class="operator">,</span> <span class="string">'http://proxy.sn.no:8001/'</span><span class="operator">);</span>
<span class="variable">$ua</span><span class="operator">-></span><span class="variable">proxy</span><span class="operator">(</span><span class="string">'gopher'</span><span class="operator">,</span> <span class="string">'http://proxy.sn.no:8001/'</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>The first form specifies that the URL is to be used for proxying of
access methods listed in the list in the first method argument,
i.e. 'http' and 'ftp'.</p>
</dd>
<dd>
<p>The second form shows a shorthand form for specifying
proxy URL for a single access scheme.</p>
</dd>
</li>
<dt><strong><a name="item_no_proxy">$ua->no_proxy( $domain, ... )</a></strong>
<dd>
<p>Do not proxy requests to the given domains. Calling no_proxy without
any domains clears the list of domains. Eg:</p>
</dd>
<dd>
<pre>
<span class="variable">$ua</span><span class="operator">-></span><span class="variable">no_proxy</span><span class="operator">(</span><span class="string">'localhost'</span><span class="operator">,</span> <span class="string">'no'</span><span class="operator">,</span> <span class="operator">...);</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_env_proxy">$ua->env_proxy</a></strong>
<dd>
<p>Load proxy settings from *_proxy environment variables. You might
specify proxies like this (sh-syntax):</p>
</dd>
<dd>
<pre>
gopher_proxy=<a href="http://proxy.my.place/">http://proxy.my.place/</a>
wais_proxy=<a href="http://proxy.my.place/">http://proxy.my.place/</a>
no_proxy="localhost,my.domain"
export gopher_proxy wais_proxy no_proxy</pre>
</dd>
<dd>
<p>csh or tcsh users should use the <code>setenv</code> command to define these
environment variables.</p>
</dd>
<dd>
<p>On systems with case insensitive environment variables there exists a
name clash between the CGI environment variables and the <code>HTTP_PROXY</code>
environment variable normally picked up by env_proxy(). Because of
this <code>HTTP_PROXY</code> is not honored for CGI scripts. The
<code>CGI_HTTP_PROXY</code> environment variable can be used instead.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="request_methods">REQUEST METHODS</a></h1>
<p>The methods described in this section are used to dispatch requests
via the user agent. The following request methods are provided:</p>
<dl>
<dt><strong><a name="item_get">$ua->get( $url )</a></strong>
<dt><strong>$ua->get( $url , $field_name => $value, ... )</strong>
<dd>
<p>This method will dispatch a <code>GET</code> request on the given $url. Further
arguments can be given to initialize the headers of the request. These
are given as separate name/value pairs. The return value is a
response object. See <a href="../../lib/HTTP/Response.html">the HTTP::Response manpage</a> for a description of the
interface it provides.</p>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?