📄 ch20_03.htm
字号:
<dt><b><tt class="literal">is_error</tt></b></dt><dd><a name="INDEX-2503" />Returns true when the response code is400-599. When an error occurs, you might want to use<tt class="literal">error_as_HTML</tt> to generate an HTML explanation ofthe error.</p></dd></dl><p>HTTP::Status exports the following constant functions to use asmnemonic substitutes for status codes. For example, you could dosomething like<a name="INDEX-2504" />:</p><blockquote><pre class="code">if ($rc = RC_OK) {....}</pre></blockquote><p>Here are the mnemonics, followed by the status codes they represent: </p><blockquote><pre class="code">RC_CONTINUE (100)RC_SWITCHING_PROTOCOLS (101)RC_OK (200)RC_CREATED (201)RC_ACCEPTED (202)RC_NON_AUTHORITATIVE_INFORMATION (203)RC_NO_CONTENT (204)RC_RESET_CONTENT (205)RC_PARTIAL_CONTENT (206)RC_MULTIPLE_CHOICES (300)RC_MOVED_PERMANENTLY (301)RC_MOVED_TEMPORARILY (302)RC_SEE_OTHER (303)RC_NOT_MODIFIED (304)RC_USE_PROXY (305)RC_BAD_REQUEST (400)RC_UNAUTHORIZED (401)RC_PAYMENT_REQUIRED (402)RC_FORBIDDEN (403)RC_NOT_FOUND (404)RC_METHOD_NOT_ALLOWED (405)RC_NOT_ACCEPTABLE (406)RC_PROXY_AUTHENTICATION_REQUIRED (407)RC_REQUEST_TIMEOUT (408)RC_CONFLICT (409)RC_GONE (410)RC_LENGTH_REQUIRED (411)RC_PRECONDITION_FAILED (412)RC_REQUEST_ENTITY_TOO_LARGE (413)RC_REQUEST_URI_TOO_LARGE (414)RC_UNSUPPORTED_MEDIA_TYPE (415)RC_REQUEST_RANGE_NOT_SATISFIABLE (416)RC_INTERNAL_SERVER_ERROR (500)RC_NOT_IMPLEMENTED (501)RC_BAD_GATEWAY (502)RC_SERVICE_UNAVAILABLE (503)RC_GATEWAY_TIMEOUT (504)RC_HTTP_VERSION_NOT_SUPPORTED (505)</pre></blockquote></div><a name="perlnut2-CHP-20-SECT-3.5" /><div class="sect2"><h3 class="sect2">20.3.5. HTTP::Date</h3><a name="INDEX-2505" /><a name="INDEX-2506" /><p><a name="INDEX-2507" /><a name="INDEX-2508" /><a name="INDEX-2509" />The HTTP::Date module is useful whenyou want to process a date string. It exports two functions thatconvert date strings to and from standard time formats.</p><a name="INDEX-2510" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>parse_date</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>parse_date($<em class="replaceable">str</em>)</pre><p><a name="INDEX-2510" />Parses a date string and returnsit as a list of numerical values followed by a time zonespecification. If the date is unrecognized, then the empty list isreturned.</p></div><a name="INDEX-2511" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>str2time</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>str2time(<em class="replaceable">str</em> [, <em class="replaceable">zone</em>])</pre><p><a name="INDEX-2511" />Converts the time specified as astring in the first parameter into the number of seconds since epoch.This function recognizes a wide variety of formats, including RFC1123 (standard HTTP), RFC 850, ANSI C <tt class="literal">asctime</tt>,common log file format, Unix <em class="emphasis">ls -l</em>, and Windows<em class="emphasis">dir</em>, among others. When a time zone is notimplicit in the first parameter, this function will use an optionaltime zone specified as the second parameter, such as -0800, +0500, orGMT. If the second parameter is omitted, and the time zone isambiguous, the local time zone is used.</p></div><a name="INDEX-2512" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>time2iso</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>time2iso([$<em class="replaceable">time</em>])</pre><p><a name="INDEX-2512" />Same as <tt class="literal">time2str()</tt>, but returns a "YYYY-MM-DDhh:mm:ss"-formatted string representing time in thelocal time zone.</p></div><a name="INDEX-2513" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>time2isoz</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>time2isoz([$<em class="replaceable">time</em>])</pre><p><a name="INDEX-2513" />Same as <tt class="literal">time2str()</tt>, but returns a "YYYY-MM-DDhh:mm:ssZ"-formatted string representing UniversalTime.</p></div><a name="INDEX-2514" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>time2str</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>time2str([<em class="replaceable">time</em>])</pre><p><a name="INDEX-2514" />Given the number of seconds sincemachine epoch, this function generates the equivalent time asspecified in RFC 1123, which is the recommended time format used inHTTP. When invoked with no parameter, the current time is used.</p></div></div><a name="perlnut2-CHP-20-SECT-3.6" /><div class="sect2"><h3 class="sect2">20.3.6. HTTP::Cookies</h3><p><a name="INDEX-2515" /><a name="INDEX-2516" /><a name="INDEX-2517" />HTTPcookies provide a mechanism for preserving information about a clientor user across several different visits to a site or page. The"cookie" is a name/value pair sentto the client on its initial visit to a page. This cookie is storedby the client and sent back in the request upon revisit to the samepage.</p><p><a name="INDEX-2518" /><a name="INDEX-2519" />A server initializesa cookie with the Set-Cookie header. Set-Cookie sets the name andvalue of a cookie, as well as other parameters such as how long thecookie is valid and the range of URLs to which the cookie applies.Each cookie (a single name/value pair) is sent in its own Set-Cookieheader, so if there is more than one cookie sent to a client,multiple Set-Cookie headers are sent in the response. Two Set-Cookieheaders may be used in server responses: Set-Cookie is defined in theoriginal Netscape cookie specification, and Set-Cookie2 is thelatest, IETF-defined header. Both header styles are supported byHTTP::Cookies. The latest browsers also support both styles.</p><p>If a client visits a page for which it has a valid cookie stored, theclient <a name="INDEX-2520" />sendsthe cookie in the request with the Cookie header. Thisheader's value contains any name/value pairs thatapply to the URL. Multiple cookies are separated by semicolons in theheader.</p><p>The HTTP::Cookies module is used to retrieve, return, and manage thecookies used by an LWP::UserAgent client application. Setting cookiesfrom an LWP-created server requires only the coding of the properresponse headers sent by an HTTP::Daemon server application.HTTP::Cookies is not designed to be used in setting cookies on theserver side, although you may find use for it in managing sentcookies.</p><p><a name="INDEX-2521" />The <tt class="literal">new</tt> constructorfor HTTP::Cookies creates an object called a cookie jar, whichrepresents a collection of saved cookies usually read from a file.Methods on the cookie jar object allow you to add new cookies or sendcookie information in a client request to a specific URL. Theconstructor may take optional parameters, as shown in the followingexample:</p><blockquote><pre class="code">$cjar = HTTP::Cookies->new( file => 'cookies.txt', autosave => 1, ignore_discard => 0 );</pre></blockquote><p>The cookie jar object <tt class="literal">$cjar</tt> created here containsany cookie information stored in the file<em class="emphasis">cookies.txt</em>. The <tt class="literal">autosave</tt>parameter takes a Boolean value that determines if the state of thecookie jar is saved to the file upon destruction of the object.<tt class="literal">ignore_discard</tt> also takes a Boolean value todetermine if cookies marked to be discarded are still saved to thefile.</p><p>Cookies received by a client are added to the cookie jar with the<tt class="literal">extract_cookies</tt> method. This method searches anHTTP::Response object for Set-Cookie and Set-Cookie2 headers and addsthem to the cookie jar. Cookies are sent in a client request usingthe <tt class="literal">add-cookie-header</tt> method. This method takes anHTTP::Request object with the URL component already set, and if theURL matches any entries in the cookie jar, adds the appropriateCookie headers to the request.</p><p>These methods can be used on a cookie jar object created byHTTP::Cookies.</p><a name="INDEX-2522" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>add_cookie_header</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">cjar</em>->add_cookie_header($<em class="replaceable">request</em>)</pre><p><a name="INDEX-2522" />Adds appropriate Cookieheaders to an HTTP::Request object<tt class="literal">$</tt><em class="replaceable"><tt>request</tt></em>.<tt class="literal">$</tt><em class="replaceable"><tt>request</tt></em> must alreadybe created with a valid URL address. This method will search thecookie jar for any cookies matching the request URL. If the cookiesare valid (i.e., have not expired), they are used to create Cookieheaders and are added to the request.</p></div><a name="INDEX-2523" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>as_string</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">cjar</em>->as_string([<em class="replaceable">discard</em>])</pre><p><a name="INDEX-2523" />Returns the current contents of thecookie jar as a string. Each cookie is output as a Set-Cookie3 headerline followed by "0". If<em class="replaceable"><tt>discard</tt></em> is given and is true, cookiesmarked to be discarded will not be output. Set-Cookie3 is a specialLWP format used to store cookie information in the save file.</p></div><a name="INDEX-2524" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>clear</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">cjar</em>->clear( [<em class="replaceable">domain</em>, [<em class="replaceable">path</em>, [<em class="replaceable">key</em>] ] ])</pre><p><a name="INDEX-2524" />Without arguments, this methodclears the entire contents of the cookie jar. Given arguments,cookies belonging to a specific <em class="replaceable"><tt>domain</tt></em>,<em class="replaceable"><tt>path</tt></em>, or with a name,<em class="replaceable"><tt>key</tt></em>, will be cleared. The arguments areordered for increasing specificity. If only one argument is given,all cookies for that domain will be deleted. A second argumentspecifies a distinct <em class="replaceable"><tt>path</tt></em> within the<em class="replaceable"><tt>domain</tt></em>. To remove a cookie by keyname, youmust use all three arguments.</p></div><a name="INDEX-2525" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>extract_cookies</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">cjar</em>->extract_cookies($<em class="replaceable">response</em>)</pre><p><a name="INDEX-2525" />Searches an HTTP::Response
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -