📄 lwp.3
字号:
\&\& # Pass request to the user agent and get a response back\& my $res = $ua\->request($req);\&\& # Check the outcome of the response\& if ($res\->is_success) {\& print $res\->content;\& }\& else {\& print $res\->status_line, "\en";\& }.Ve.PPThe \f(CW$ua\fR is created once when the application starts up. New requestobjects should normally created for each request sent..SH "NETWORK SUPPORT".IX Header "NETWORK SUPPORT"This section discusses the various protocol schemes andthe \s-1HTTP\s0 style methods that headers may be used for each..PPFor all requests, a \*(L"User-Agent\*(R" header is added and initialized fromthe \f(CW$ua\fR\->agent attribute before the request is handed to the networklayer. In the same way, a \*(L"From\*(R" header is initialized from the\&\f(CW$ua\fR\->from attribute..PPFor all responses, the library adds a header called \*(L"Client-Date\*(R".This header holds the time when the response was received byyour application. The format and semantics of the header are thesame as the server created \*(L"Date\*(R" header. You may also encounter other\&\*(L"Client-XXX\*(R" headers. They are all generated by the libraryinternally and are not received from the servers..Sh "\s-1HTTP\s0 Requests".IX Subsection "HTTP Requests"\&\s-1HTTP\s0 requests are just handed off to an \s-1HTTP\s0 server and itdecides what happens. Few servers implement methods beside the usual\&\*(L"\s-1GET\s0\*(R", \*(L"\s-1HEAD\s0\*(R", \*(L"\s-1POST\s0\*(R" and \*(L"\s-1PUT\s0\*(R", but CGI-scripts may implementany method they like..PPIf the server is not available then the library will generate aninternal error response..PPThe library automatically adds a \*(L"Host\*(R" and a \*(L"Content-Length\*(R" headerto the \s-1HTTP\s0 request before it is sent over the network..PPFor a \s-1GET\s0 request you might want to add a \*(L"If-Modified-Since\*(R" or\&\*(L"If-None-Match\*(R" header to make the request conditional..PPFor a \s-1POST\s0 request you should add the \*(L"Content-Type\*(R" header. When youtry to emulate \s-1HTML\s0 <\s-1FORM\s0> handling you should usually let the valueof the \*(L"Content-Type\*(R" header be \*(L"application/x\-www\-form\-urlencoded\*(R".See lwpcook for examples of this..PPThe libwww-perl \s-1HTTP\s0 implementation currently support the \s-1HTTP/1\s0.1and \s-1HTTP/1\s0.0 protocol..PPThe library allows you to access proxy server through \s-1HTTP\s0. Thismeans that you can set up the library to forward all types of requestthrough the \s-1HTTP\s0 protocol module. See LWP::UserAgent fordocumentation of this..Sh "\s-1HTTPS\s0 Requests".IX Subsection "HTTPS Requests"\&\s-1HTTPS\s0 requests are \s-1HTTP\s0 requests over an encrypted network connectionusing the \s-1SSL\s0 protocol developed by Netscape. Everything about \s-1HTTP\s0requests above also apply to \s-1HTTPS\s0 requests. In addition the librarywill add the headers \*(L"Client-SSL-Cipher\*(R", \*(L"Client-SSL-Cert-Subject\*(R" and\&\*(L"Client-SSL-Cert-Issuer\*(R" to the response. These headers denote theencryption method used and the name of the server owner..PPThe request can contain the header \*(L"If-SSL-Cert-Subject\*(R" in order tomake the request conditional on the content of the server certificate.If the certificate subject does not match, no request is sent to theserver and an internally generated error response is returned. Thevalue of the \*(L"If-SSL-Cert-Subject\*(R" header is interpreted as a Perlregular expression..Sh "\s-1FTP\s0 Requests".IX Subsection "FTP Requests"The library currently supports \s-1GET\s0, \s-1HEAD\s0 and \s-1PUT\s0 requests. \s-1GET\s0retrieves a file or a directory listing from an \s-1FTP\s0 server. \s-1PUT\s0stores a file on a ftp server..PPYou can specify a ftp account for servers that want this in additionto user name and password. This is specified by including an \*(L"Account\*(R"header in the request..PPUser name/password can be specified using basic authorization or beencoded in the \s-1URL\s0. Failed logins return an \s-1UNAUTHORIZED\s0 response with\&\*(L"WWW-Authenticate: Basic\*(R" and can be treated like basic authorizationfor \s-1HTTP\s0..PPThe library supports ftp \s-1ASCII\s0 transfer mode by specifying the \*(L"type=a\*(R"parameter in the \s-1URL\s0. It also supports transfer of ranges for \s-1FTP\s0 transfersusing the \*(L"Range\*(R" header..PPDirectory listings are by default returned unprocessed (as returnedfrom the ftp server) with the content media type reported to be\&\*(L"text/ftp\-dir\-listing\*(R". The \f(CW\*(C`File::Listing\*(C'\fR module provides methodsfor parsing of these directory listing..PPThe ftp module is also able to convert directory listings to \s-1HTML\s0 andthis can be requested via the standard \s-1HTTP\s0 content negotiationmechanisms (add an \*(L"Accept: text/html\*(R" header in the request if youwant this)..PPFor normal file retrievals, the \*(L"Content-Type\*(R" is guessed based on thefile name suffix. See LWP::MediaTypes..PPThe \*(L"If-Modified-Since\*(R" request header works for servers that implementthe \s-1MDTM\s0 command. It will probably not work for directory listings though..PPExample:.PP.Vb 2\& $req = HTTP::Request\->new(GET => \*(Aqftp://me:passwd@ftp.some.where.com/\*(Aq);\& $req\->header(Accept => "text/html, */*;q=0.1");.Ve.Sh "News Requests".IX Subsection "News Requests"Access to the \s-1USENET\s0 News system is implemented through the \s-1NNTP\s0protocol. The name of the news server is obtained from the\&\s-1NNTP_SERVER\s0 environment variable and defaults to \*(L"news\*(R". It is notpossible to specify the hostname of the \s-1NNTP\s0 server in news: URLs..PPThe library supports \s-1GET\s0 and \s-1HEAD\s0 to retrieve news articles through the\&\s-1NNTP\s0 protocol. You can also post articles to newsgroups by using(surprise!) the \s-1POST\s0 method..PP\&\s-1GET\s0 on newsgroups is not implemented yet..PPExamples:.PP.Vb 1\& $req = HTTP::Request\->new(GET => \*(Aqnews:abc1234@a.sn.no\*(Aq);\&\& $req = HTTP::Request\->new(POST => \*(Aqnews:comp.lang.perl.test\*(Aq);\& $req\->header(Subject => \*(AqThis is a test\*(Aq,\& From => \*(Aqme@some.where.org\*(Aq);\& $req\->content(<<EOT);\& This is the content of the message that we are sending to\& the world.\& EOT.Ve.Sh "Gopher Request".IX Subsection "Gopher Request"The library supports the \s-1GET\s0 and \s-1HEAD\s0 methods for gopher requests. Allrequest header values are ignored. \s-1HEAD\s0 cheats and returns aresponse without even talking to server..PPGopher menus are always converted to \s-1HTML\s0..PPThe response \*(L"Content-Type\*(R" is generated from the document typeencoded (as the first letter) in the request \s-1URL\s0 path itself..PPExample:.PP.Vb 1\& $req = HTTP::Request\->new(GET => \*(Aqgopher://gopher.sn.no/\*(Aq);.Ve.Sh "File Request".IX Subsection "File Request"The library supports \s-1GET\s0 and \s-1HEAD\s0 methods for file requests. The\&\*(L"If-Modified-Since\*(R" header is supported. All other headers areignored. The \fIhost\fR component of the file \s-1URL\s0 must be empty or setto \*(L"localhost\*(R". Any other \fIhost\fR value will be treated as an error..PPDirectories are always converted to an \s-1HTML\s0 document. For normalfiles, the \*(L"Content-Type\*(R" and \*(L"Content-Encoding\*(R" in the response areguessed based on the file suffix..PPExample:.PP.Vb 1\& $req = HTTP::Request\->new(GET => \*(Aqfile:/etc/passwd\*(Aq);.Ve.Sh "Mailto Request".IX Subsection "Mailto Request"You can send (aka \*(L"\s-1POST\s0\*(R") mail messages using the library. Allheaders specified for the request are passed on to the mail system.The \*(L"To\*(R" header is initialized from the mail address in the \s-1URL\s0..PPExample:.PP.Vb 3\& $req = HTTP::Request\->new(POST => \*(Aqmailto:libwww@perl.org\*(Aq);\& $req\->header(Subject => "subscribe");\& $req\->content("Please subscribe me to the libwww\-perl mailing list!\en");.Ve.Sh "\s-1CPAN\s0 Requests".IX Subsection "CPAN Requests"URLs with scheme \f(CW\*(C`cpan:\*(C'\fR are redirected to the a suitable \s-1CPAN\s0mirror. If you have your own local mirror of \s-1CPAN\s0 you might tell \s-1LWP\s0to use it for \f(CW\*(C`cpan:\*(C'\fR URLs by an assignment like this:.PP.Vb 1\& $LWP::Protocol::cpan::CPAN = "file:/local/CPAN/";.Ve.PPSuitable \s-1CPAN\s0 mirrors are also picked up from the configuration forthe \s-1CPAN\s0.pm, so if you have used that module a suitable mirror shouldbe picked automatically. If neither of these apply, then a redirectto the generic \s-1CPAN\s0 http location is issued..PPExample request to download the newest perl:.PP.Vb 1\& $req = HTTP::Request\->new(GET => "cpan:src/latest.tar.gz");.Ve.SH "OVERVIEW OF CLASSES AND PACKAGES".IX Header "OVERVIEW OF CLASSES AND PACKAGES"This table should give you a quick overview of the classes provided by thelibrary. Indentation shows class inheritance..PP.Vb 8\& LWP::MemberMixin \-\- Access to member variables of Perl5 classes\& LWP::UserAgent \-\- WWW user agent class\& LWP::RobotUA \-\- When developing a robot applications\& LWP::Protocol \-\- Interface to various protocol schemes\& LWP::Protocol::http \-\- http:// access\& LWP::Protocol::file \-\- file:// access\& LWP::Protocol::ftp \-\- ftp:// access\& ...\&\& LWP::Authen::Basic \-\- Handle 401 and 407 responses\& LWP::Authen::Digest\&\& HTTP::Headers \-\- MIME/RFC822 style header (used by HTTP::Message)\& HTTP::Message \-\- HTTP style message\& HTTP::Request \-\- HTTP request\& HTTP::Response \-\- HTTP response\& HTTP::Daemon \-\- A HTTP server class\&\& WWW::RobotRules \-\- Parse robots.txt files\& WWW::RobotRules::AnyDBM_File \-\- Persistent RobotRules\&\& Net::HTTP \-\- Low level HTTP client.Ve.PPThe following modules provide various functions and definitions..PP.Vb 9\& LWP \-\- This file. Library version number and documentation.\& LWP::MediaTypes \-\- MIME types configuration (text/html etc.)\& LWP::Debug \-\- Debug logging module\& LWP::Simple \-\- Simplified procedural interface for common functions\& HTTP::Status \-\- HTTP status code (200 OK etc)\& HTTP::Date \-\- Date parsing module for HTTP date formats\& HTTP::Negotiate \-\- HTTP content negotiation calculation\& File::Listing \-\- Parse directory listings\& HTML::Form \-\- Processing for <form>s in HTML documents.Ve.SH "MORE DOCUMENTATION".IX Header "MORE DOCUMENTATION"All modules contain detailed information on the interfaces theyprovide. The \fIlwpcook\fR manpage is the libwww-perl cookbook that containexamples of typical usage of the library. You might want to take alook at how the scripts \f(CW\*(C`lwp\-request\*(C'\fR, \f(CW\*(C`lwp\-rget\*(C'\fR and \f(CW\*(C`lwp\-mirror\*(C'\fRare implemented..SH "ENVIRONMENT".IX Header "ENVIRONMENT"The following environment variables are used by \s-1LWP:\s0.IP "\s-1HOME\s0" 4.IX Item "HOME"The \f(CW\*(C`LWP::MediaTypes\*(C'\fR functions will look for the \fI.media.types\fR and\&\fI.mime.types\fR files relative to you home directory..IP "http_proxy" 4.IX Item "http_proxy".PD 0.IP "ftp_proxy" 4.IX Item "ftp_proxy".IP "xxx_proxy" 4.IX Item "xxx_proxy".IP "no_proxy" 4.IX Item "no_proxy".PDThese environment variables can be set to enable communication througha proxy server. See the description of the \f(CW\*(C`env_proxy\*(C'\fR method inLWP::UserAgent..IP "\s-1PERL_LWP_USE_HTTP_10\s0" 4.IX Item "PERL_LWP_USE_HTTP_10"Enable the old \s-1HTTP/1\s0.0 protocol driver instead of the new \s-1HTTP/1\s0.1driver. You might want to set this to a \s-1TRUE\s0 value if you discoverthat your old \s-1LWP\s0 applications fails after you installed \s-1LWP\-5\s0.60 orbetter..IP "\s-1PERL_HTTP_URI_CLASS\s0" 4.IX Item "PERL_HTTP_URI_CLASS"Used to decide what \s-1URI\s0 objects to instantiate. The default is \f(CW\*(C`URI\*(C'\fR.You might want to set it to \f(CW\*(C`URI::URL\*(C'\fR for compatibility with old times..SH "AUTHORS".IX Header "AUTHORS"\&\s-1LWP\s0 was made possible by contributions from Adam Newby, AlbertDvornik, Alexandre Duret-Lutz, Andreas Gustafsson, Andreas Ko\*:nig,Andrew Pimlott, Andy Lester, Ben Coleman, Benjamin Low, Ben Low, BenTilly, Blair Zajac, Bob Dalgleish, BooK, Brad Hughes, BrianJ. Murrell, Brian McCauley, Charles C. Fu, Charles Lane, Chris Nandor,Christian Gilmore, Chris W. Unger, Craig Macdonald, Dale Couch, DanKubb, Dave Dunkin, Dave W. Smith, David Coppit, David Dick, DavidD. Kilzer, Doug MacEachern, Edward Avis, erik, Gary Shea, Gisle Aas,Graham Barr, Gurusamy Sarathy, Hans de Graaff, Harald Joerg, HarryBochner, Hugo, Ilya Zakharevich, \s-1INOUE\s0 Yoshinari, Ivan Panchenko, JackShirazi, James Tillman, Jan Dubois, Jared Rhine, Jim Stern, JoaoLopes, John Klar, Johnny Lee, Josh Kronengold, Josh Rai, JoshuaChamas, Joshua Hoblitt, Kartik Subbarao, Keiichiro Nagano, KenWilliams, \s-1KONISHI\s0 Katsuhiro, Lee T Lindley, Liam Quinn, Marc Hedlund,Marc Langheinrich, Mark D. Anderson, Marko Asplund, Mark Stosberg,Markus B Kru\*:ger, Markus Laker, Martijn Koster, Martin Thurn, MatthewEldridge, Matthew.van.Eerde, Matt Sergeant, Michael A. Chase, MichaelQuaranta, Michael Thompson, Mike Schilli, Moshe Kaminsky, NathanTorkington, Nicolai Langfeldt, Norton Allen, Olly Betts, PaulJ. Schinder, peterm, Philip GuentherDaniel Buenzli, Pon Hwa Lin,Radoslaw Zielinski, Radu Greab, Randal L. Schwartz, Richard Chen,Robin Barker, Roy Fielding, Sander van Zoest, Sean M. Burke,shildreth, Slaven Rezic, Steve A Fink, Steve Hay, Steven Butler,Steve_Kilbane, Takanori Ugai, Thomas Lotterer, Tim Bunce, Tom Hughes,Tony Finch, Ville Skytta\*:, Ward Vandewege, William York, Yale Huang,and Yitzchak Scott-Thoennes..PP\&\s-1LWP\s0 owes a lot in motivation, design, and code, to the libwww-perllibrary for Perl4 by Roy Fielding, which included work from AlbertoAccomazzi, James Casey, Brooks Cutter, Martijn Koster, OscarNierstrasz, Mel Melchner, Gertjan van Oosten, Jared Rhine, JackShirazi, Gene Spafford, Marc VanHeyningen, Steven E. Brenner, MarionHakanson, Waldemar Kebsch, Tony Sanders, and Larry Wall; see thelibwww\-perl\-0.40 library for details..SH "COPYRIGHT".IX Header "COPYRIGHT".Vb 2\& Copyright 1995\-2005, Gisle Aas\& Copyright 1995, Martijn Koster.Ve.PPThis library is free software; you can redistribute it and/ormodify it under the same terms as Perl itself..SH "AVAILABILITY".IX Header "AVAILABILITY"The latest version of this library is likely to be available from \s-1CPAN\s0as well as:.PP.Vb 1\& http://www.linpro.no/lwp/.Ve.PPThe best place to discuss this code is on the <libwww@perl.org>mailing list.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -