📄 ch20_03.htm
字号:
<html><head><title>The HTTP Modules (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch20_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch20_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">20.3. The HTTP Modules</h2><p>The HTTP modules implement an interface to the HTTP messagingprotocol used in web transactions. Its most useful modules areHTTP::Request and HTTP::Response, which create objects for clientrequests and server responses. Other modules provide means formanipulating headers, interpreting server response codes, managingcookies, converting date formats, and creating basic serverapplications.</p><p>Client applications created with LWP::UserAgent use HTTP::Requestobjects to create and send requests to servers. The informationreturned from a server is saved as an HTTP::Response object. Both ofthese objects are subclasses of HTTP::Message, which provides generalmethods of creating and modifying HTTP messages. The headerinformation included in HTTP messages can be represented by objectsof the HTTP::Headers class.</p><p>HTTP::Status includes functions to classify response codes into thecategories of informational, successful, redirection, error, clienterror, or server error. It also exports symbolic aliases of HTTPresponse codes; one could refer to the status code of 200 as RC_OKand refer to 404 as RC_NOT_FOUND.</p><p>The HTTP::Date module converts date strings from and to machine time.The HTTP::Daemon module can be used to create web serverapplications, utilizing the functionality of the rest of the LWPmodules to communicate with clients.</p><a name="perlnut2-CHP-20-SECT-3.1" /><div class="sect2"><h3 class="sect2">20.3.1. HTTP::Request</h3><p><a name="INDEX-2452" /><a name="INDEX-2453" /><a name="INDEX-2454" /><a name="INDEX-2455" /><a name="INDEX-2456" /><a name="INDEX-2457" />This module summarizes a webclient's request. For a simple GET request, youdefine an object with the GET method and assign a URL to apply it to.Basic headers would be filled in automatically by LWP. For a POST orPUT request, you might want to specify a custom HTTP::Headers objectfor the request, or use the contents of a file for an entity body.Since HTTP::Request inherits everything in HTTP::Message, you can usethe header and entity body manipulation methods from HTTP::Message inHTTP::Request objects.</p><p><a name="INDEX-2458" />The constructor for HTTP::Request lookslike this:</p><blockquote><pre class="code">$req = http::Request->new (<em class="replaceable"><tt>method</em>, <em class="replaceable">url</em>, [$<em class="replaceable">header</em>, [<em class="replaceable">content</tt></em>]]);</pre></blockquote><p>The method and URL values for the request are required parameters.The header and content arguments are not required, nor even necessaryfor all requests. The parameters are defined as follows:</p><dl><dt><i><em class="replaceable"><tt>method</tt></em></i></dt><dd>A string specifying the HTTP request method. GET, HEAD, and POST arethe most commonly used. Other methods defined in the HTTPspecification such as PUT and DELETE are not supported by mostservers.</p></dd><dt><i><em class="replaceable"><tt>url</tt></em></i></dt><dd>The address and resource name of the information you are requesting.This argument may be either a string containing an absolute URL (thehostname is required), or a URI::URL object that stores all theinformation about the URL.</p></dd><dt><b><tt class="literal">$</tt><em class="replaceable">header</em></b></dt><dd>A reference to an HTTP::Headers object.</p></dd><dt><i><em class="replaceable"><tt>content</tt></em></i></dt><dd>A scalar that specifies the entity body of the request. If omitted,the entity body is empty.</p></dd></dl><p>The following methods can be used on HTTP::Request objects.</p><a name="INDEX-2459" /><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">req</em>->as_string</pre><p><a name="INDEX-2459" />Returns a text version of the requestobject as a string with <tt class="literal">\n</tt> placed after each line.Information about the object reference is also included in the firstline. The returned string looks like this:</p><blockquote><pre class="code">-- HTTP::Request=HASH(0x68148) --PUT http:www.ora.com/example/hi.textContent-Length: 2Content-Type: text/plainhi------------------</pre></blockquote></div><a name="INDEX-2460" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>method</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">req</em>->method ([<em class="replaceable">method</em>])</pre><p><a name="INDEX-2460" />Sets or retrieves the HTTPmethod for an HTTP::Request object. Without an argument,<tt class="literal">method</tt> returns the object'scurrent method.</p></div><a name="INDEX-2461" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>url</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">req</em>->url ([<em class="replaceable">url</em>])</pre><p><a name="INDEX-2461" />Sets or retrieves the URL for therequest object. Without an argument, this method retrieves thecurrent URL for the object. <em class="replaceable"><tt>url</tt></em> is astring containing the new URL to set for the request or a URI::URLobject.</p></div></div><a name="perlnut2-CHP-20-SECT-3.2" /><div class="sect2"><h3 class="sect2">20.3.2. HTTP::Response</h3><p><a name="INDEX-2462" /><a name="INDEX-2463" /><a name="INDEX-2464" />Responses from a web server are describedby HTTP::Response objects. An HTTP response message contains a statusline, headers, and any content data that was requested by the client(such as an HTML file). The status line is the minimum requirementfor a response. It contains the version of HTTP that the server isrunning, a status code indicating the success, failure, or othercondition the request received from the server, and a short messagedescribing the status code.</p><p>If LWP has problems fulfilling your request, it internally generatesan HTTP::Response object and fills in an appropriate response code.In the context of web client programming, you'llusually get an HTTP::Response object from LWP::UserAgent andLWP::RobotUA.</p><p>If you plan to write extensions to LWP or to a web server or proxyserver, you might use HTTP::Response to generate your own responses.</p><p><a name="INDEX-2465" />The constructor for HTTP::Responselooks like this:</p><blockquote><pre class="code">$resp = HTTP::Response->new (<em class="replaceable"><tt>rc</em>, [<em class="replaceable">msg</em>, [<em class="replaceable">header</em>, [<em class="replaceable">content</tt></em>]]]);</pre></blockquote><p>In its simplest form, an HTTP::Response object can contain just aresponse code. If you would like to specify a more detailed messagethan "OK" or "Notfound," you can specify a text description of theresponse code as the second parameter. As a third parameter, you canpass a reference to an HTTP::Headers object to specify the responseheaders. Finally, you can also include an entity body in the fourthparameter as a scalar.</p><p>For client applications, it is unlikely that you will build your ownresponse object with the constructor for this class. You receive aclient object when you use the <tt class="literal">request</tt> method onan LWP::UserAgent object. For example:</p><blockquote><pre class="code">$ua = LWP::UserAgent->new;$req = HTTP::Request->new(GET, $url)$resp = $ua->request($req);</pre></blockquote><p>The server's response is contained in the object<tt class="literal">$resp</tt>. When you have this object, you can use theHTTP::Response methods to get the information about the response.Since HTTP::Response is a subclass of HTTP::Message, you can also usemethods from that class on response objects. See <a href="ch20_03.htm#perlnut2-CHP-20-SECT-3.8">Section 20.3.8, "HTTP::Message"</a> for a description of itsmethods.</p><p>The following methods can be used on objects created byHTTP::Response.</p><a name="INDEX-2466" /><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">resp</em>->as_string( )</pre><p><a name="INDEX-2466" />Returns a string version of theresponse with lines separated by <tt class="literal">\n</tt>. For example,this method would return a response string that looks like this:</p><blockquote><pre class="code">-- HTTP::Response=HASH(0xc8548) --RC: 200 (OK)Message: all is fineContent-Length: 2Content-Type: text/plainhi------------------</pre></blockquote></div><a name="INDEX-2467" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>base</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">resp</em>->base( )</pre><p><a name="INDEX-2467" />Returns the base URL of the response.If the response was hypertext, any links from the hypertext should berelative to the location returned by this method. LWP looks for theBASE tag in HTML and Content-Base/Content-Location HTTP headers for abase specification. If a base was not explicitly defined by theserver, LWP uses the requesting URL as the base.</p></div><a name="INDEX-2468" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>code</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">resp</em>->code ([<em class="replaceable">code</em>])</pre><p><a name="INDEX-2468" />When invoked without anyparameters, this method returns the object'sresponse code. Sets the status code of the object when invoked withan argument.</p></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -