📄 lwp::useragent.3
字号:
\& wais_proxy=http://proxy.my.place/\& no_proxy="localhost,my.domain"\& export gopher_proxy wais_proxy no_proxy.Ve.Spcsh or tcsh users should use the \f(CW\*(C`setenv\*(C'\fR command to define theseenvironment variables..SpOn systems with case insensitive environment variables there exists aname clash between the \s-1CGI\s0 environment variables and the \f(CW\*(C`HTTP_PROXY\*(C'\fRenvironment variable normally picked up by \fIenv_proxy()\fR. Because ofthis \f(CW\*(C`HTTP_PROXY\*(C'\fR is not honored for \s-1CGI\s0 scripts. The\&\f(CW\*(C`CGI_HTTP_PROXY\*(C'\fR environment variable can be used instead..SH "REQUEST METHODS".IX Header "REQUEST METHODS"The methods described in this section are used to dispatch requestsvia the user agent. The following request methods are provided:.ie n .IP "$ua\fR\->get( \f(CW$url )" 4.el .IP "\f(CW$ua\fR\->get( \f(CW$url\fR )" 4.IX Item "$ua->get( $url )".PD 0.ie n .IP "$ua\fR\->get( \f(CW$url\fR , \f(CW$field_name\fR => \f(CW$value, ... )" 4.el .IP "\f(CW$ua\fR\->get( \f(CW$url\fR , \f(CW$field_name\fR => \f(CW$value\fR, ... )" 4.IX Item "$ua->get( $url , $field_name => $value, ... )".PDThis method will dispatch a \f(CW\*(C`GET\*(C'\fR request on the given \f(CW$url\fR. Furtherarguments can be given to initialize the headers of the request. Theseare given as separate name/value pairs. The return value is aresponse object. See HTTP::Response for a description of theinterface it provides..SpFields names that start with \*(L":\*(R" are special. These will notinitialize headers of the request but will determine how the responsecontent is treated. The following special field names are recognized:.Sp.Vb 3\& :content_file => $filename\& :content_cb => \e&callback\& :read_size_hint => $bytes.Ve.SpIf a \f(CW$filename\fR is provided with the \f(CW\*(C`:content_file\*(C'\fR option, then theresponse content will be saved here instead of in the responseobject. If a callback is provided with the \f(CW\*(C`:content_cb\*(C'\fR option thenthis function will be called for each chunk of the response content asit is received from the server. If neither of these options aregiven, then the response content will accumulate in the responseobject itself. This might not be suitable for very large responsebodies. Only one of \f(CW\*(C`:content_file\*(C'\fR or \f(CW\*(C`:content_cb\*(C'\fR can bespecified. The content of unsuccessful responses will alwaysaccumulate in the response object itself, regardless of the\&\f(CW\*(C`:content_file\*(C'\fR or \f(CW\*(C`:content_cb\*(C'\fR options passed in..SpThe \f(CW\*(C`:read_size_hint\*(C'\fR option is passed to the protocol module whichwill try to read data from the server in chunks of this size. Asmaller value for the \f(CW\*(C`:read_size_hint\*(C'\fR will result in a highernumber of callback invocations..SpThe callback function is called with 3 arguments: a chunk of data, areference to the response object, and a reference to the protocolobject. The callback can abort the request by invoking \fIdie()\fR. Theexception message will show up as the \*(L"X\-Died\*(R" header field in theresponse returned by the \fIget()\fR function..ie n .IP "$ua\fR\->head( \f(CW$url )" 4.el .IP "\f(CW$ua\fR\->head( \f(CW$url\fR )" 4.IX Item "$ua->head( $url )".PD 0.ie n .IP "$ua\fR\->head( \f(CW$url\fR , \f(CW$field_name\fR => \f(CW$value, ... )" 4.el .IP "\f(CW$ua\fR\->head( \f(CW$url\fR , \f(CW$field_name\fR => \f(CW$value\fR, ... )" 4.IX Item "$ua->head( $url , $field_name => $value, ... )".PDThis method will dispatch a \f(CW\*(C`HEAD\*(C'\fR request on the given \f(CW$url\fR.Otherwise it works like the \fIget()\fR method described above..ie n .IP "$ua\fR\->post( \f(CW$url, \e%form )" 4.el .IP "\f(CW$ua\fR\->post( \f(CW$url\fR, \e%form )" 4.IX Item "$ua->post( $url, %form )".PD 0.ie n .IP "$ua\fR\->post( \f(CW$url, \e@form )" 4.el .IP "\f(CW$ua\fR\->post( \f(CW$url\fR, \e@form )" 4.IX Item "$ua->post( $url, @form )".ie n .IP "$ua\fR\->post( \f(CW$url\fR, \e%form, \f(CW$field_name\fR => \f(CW$value, ... )" 4.el .IP "\f(CW$ua\fR\->post( \f(CW$url\fR, \e%form, \f(CW$field_name\fR => \f(CW$value\fR, ... )" 4.IX Item "$ua->post( $url, %form, $field_name => $value, ... )".ie n .IP "$ua\fR\->post( \f(CW$url\fR, \f(CW$field_name\fR => \f(CW$value,... Content => \e%form )" 4.el .IP "\f(CW$ua\fR\->post( \f(CW$url\fR, \f(CW$field_name\fR => \f(CW$value\fR,... Content => \e%form )" 4.IX Item "$ua->post( $url, $field_name => $value,... Content => %form )".ie n .IP "$ua\fR\->post( \f(CW$url\fR, \f(CW$field_name\fR => \f(CW$value,... Content => \e@form )" 4.el .IP "\f(CW$ua\fR\->post( \f(CW$url\fR, \f(CW$field_name\fR => \f(CW$value\fR,... Content => \e@form )" 4.IX Item "$ua->post( $url, $field_name => $value,... Content => @form )".ie n .IP "$ua\fR\->post( \f(CW$url\fR, \f(CW$field_name\fR => \f(CW$value\fR,... Content => \f(CW$content )" 4.el .IP "\f(CW$ua\fR\->post( \f(CW$url\fR, \f(CW$field_name\fR => \f(CW$value\fR,... Content => \f(CW$content\fR )" 4.IX Item "$ua->post( $url, $field_name => $value,... Content => $content )".PDThis method will dispatch a \f(CW\*(C`POST\*(C'\fR request on the given \f(CW$url\fR, with\&\f(CW%form\fR or \f(CW@form\fR providing the key/value pairs for the fill-in formcontent. Additional headers and content options are the same as forthe \fIget()\fR method..SpThis method will use the \s-1\fIPOST\s0()\fR function from \f(CW\*(C`HTTP::Request::Common\*(C'\fRto build the request. See HTTP::Request::Common for a details onhow to pass form content and other advanced features..ie n .IP "$ua\fR\->mirror( \f(CW$url\fR, \f(CW$filename )" 4.el .IP "\f(CW$ua\fR\->mirror( \f(CW$url\fR, \f(CW$filename\fR )" 4.IX Item "$ua->mirror( $url, $filename )"This method will get the document identified by \f(CW$url\fR and store it infile called \f(CW$filename\fR. If the file already exists, then the requestwill contain an \*(L"If-Modified-Since\*(R" header matching the modificationtime of the file. If the document on the server has not changed sincethis time, then nothing happens. If the document has been updated, itwill be downloaded again. The modification time of the file will beforced to match that of the server..SpThe return value is the the response object..ie n .IP "$ua\fR\->request( \f(CW$request )" 4.el .IP "\f(CW$ua\fR\->request( \f(CW$request\fR )" 4.IX Item "$ua->request( $request )".PD 0.ie n .IP "$ua\fR\->request( \f(CW$request\fR, \f(CW$content_file )" 4.el .IP "\f(CW$ua\fR\->request( \f(CW$request\fR, \f(CW$content_file\fR )" 4.IX Item "$ua->request( $request, $content_file )".ie n .IP "$ua\fR\->request( \f(CW$request\fR, \f(CW$content_cb )" 4.el .IP "\f(CW$ua\fR\->request( \f(CW$request\fR, \f(CW$content_cb\fR )" 4.IX Item "$ua->request( $request, $content_cb )".ie n .IP "$ua\fR\->request( \f(CW$request\fR, \f(CW$content_cb\fR, \f(CW$read_size_hint )" 4.el .IP "\f(CW$ua\fR\->request( \f(CW$request\fR, \f(CW$content_cb\fR, \f(CW$read_size_hint\fR )" 4.IX Item "$ua->request( $request, $content_cb, $read_size_hint )".PDThis method will dispatch the given \f(CW$request\fR object. Normally thiswill be an instance of the \f(CW\*(C`HTTP::Request\*(C'\fR class, but any object witha similar interface will do. The return value is a response object.See HTTP::Request and HTTP::Response for a description of theinterface provided by these classes..SpThe \fIrequest()\fR method will process redirects and authenticationresponses transparently. This means that it may actually send severalsimple requests via the \fIsimple_request()\fR method described below..SpThe request methods described above; \fIget()\fR, \fIhead()\fR, \fIpost()\fR and\&\fImirror()\fR, will all dispatch the request they build via this method.They are convenience methods that simply hides the creation of therequest object for you..SpThe \f(CW$content_file\fR, \f(CW$content_cb\fR and \f(CW$read_size_hint\fR all correspond tooptions described with the \fIget()\fR method above..SpYou are allowed to use a \s-1CODE\s0 reference as \f(CW\*(C`content\*(C'\fR in the requestobject passed in. The \f(CW\*(C`content\*(C'\fR function should return the contentwhen called. The content can be returned in chunks. The contentfunction will be invoked repeatedly until it return an empty string tosignal that there is no more content..ie n .IP "$ua\fR\->simple_request( \f(CW$request )" 4.el .IP "\f(CW$ua\fR\->simple_request( \f(CW$request\fR )" 4.IX Item "$ua->simple_request( $request )".PD 0.ie n .IP "$ua\fR\->simple_request( \f(CW$request\fR, \f(CW$content_file )" 4.el .IP "\f(CW$ua\fR\->simple_request( \f(CW$request\fR, \f(CW$content_file\fR )" 4.IX Item "$ua->simple_request( $request, $content_file )".ie n .IP "$ua\fR\->simple_request( \f(CW$request\fR, \f(CW$content_cb )" 4.el .IP "\f(CW$ua\fR\->simple_request( \f(CW$request\fR, \f(CW$content_cb\fR )" 4.IX Item "$ua->simple_request( $request, $content_cb )".ie n .IP "$ua\fR\->simple_request( \f(CW$request\fR, \f(CW$content_cb\fR, \f(CW$read_size_hint )" 4.el .IP "\f(CW$ua\fR\->simple_request( \f(CW$request\fR, \f(CW$content_cb\fR, \f(CW$read_size_hint\fR )" 4.IX Item "$ua->simple_request( $request, $content_cb, $read_size_hint )".PDThis method dispatches a single request and returns the responsereceived. Arguments are the same as for \fIrequest()\fR described above..SpThe difference from \fIrequest()\fR is that \fIsimple_request()\fR will not try tohandle redirects or authentication responses. The \fIrequest()\fR methodwill in fact invoke this method for each simple request it sends..ie n .IP "$ua\fR\->is_protocol_supported( \f(CW$scheme )" 4.el .IP "\f(CW$ua\fR\->is_protocol_supported( \f(CW$scheme\fR )" 4.IX Item "$ua->is_protocol_supported( $scheme )"You can use this method to test whether this user agent object supports thespecified \f(CW\*(C`scheme\*(C'\fR. (The \f(CW\*(C`scheme\*(C'\fR might be a string (like 'http' or\&'ftp') or it might be an \s-1URI\s0 object reference.).SpWhether a scheme is supported, is determined by the user agent's\&\f(CW\*(C`protocols_allowed\*(C'\fR or \f(CW\*(C`protocols_forbidden\*(C'\fR lists (if any), and bythe capabilities of \s-1LWP\s0. I.e., this will return \s-1TRUE\s0 only if \s-1LWP\s0supports this protocol \fIand\fR it's permitted for this particularobject..Sh "Callback methods".IX Subsection "Callback methods"The following methods will be invoked as requests are processed. Thesemethods are documented here because subclasses of \f(CW\*(C`LWP::UserAgent\*(C'\fRmight want to override their behaviour..ie n .IP "$ua\fR\->prepare_request( \f(CW$request )" 4.el .IP "\f(CW$ua\fR\->prepare_request( \f(CW$request\fR )" 4.IX Item "$ua->prepare_request( $request )"This method is invoked by \fIsimple_request()\fR. Its task is to modify thegiven \f(CW$request\fR object by setting up various headers based on theattributes of the user agent. The return value should normally be the\&\f(CW$request\fR object passed in. If a different request object is returnedit will be the one actually processed..SpThe headers affected by the base implementation are; \*(L"User-Agent\*(R",\&\*(L"From\*(R", \*(L"Range\*(R" and \*(L"Cookie\*(R"..ie n .IP "$ua\fR\->redirect_ok( \f(CW$prospective_request\fR, \f(CW$response )" 4.el .IP "\f(CW$ua\fR\->redirect_ok( \f(CW$prospective_request\fR, \f(CW$response\fR )" 4.IX Item "$ua->redirect_ok( $prospective_request, $response )"This method is called by \fIrequest()\fR before it tries to follow aredirection to the request in \f(CW$response\fR. This should return a \s-1TRUE\s0value if this redirection is permissible. The \f(CW$prospective_request\fRwill be the request to be sent if this method returns \s-1TRUE\s0..SpThe base implementation will return \s-1FALSE\s0 unless the methodis in the object's \f(CW\*(C`requests_redirectable\*(C'\fR list,\&\s-1FALSE\s0 if the proposed redirection is to a \*(L"file://...\*(R"\&\s-1URL\s0, and \s-1TRUE\s0 otherwise..ie n .IP "$ua\fR\->get_basic_credentials( \f(CW$realm\fR, \f(CW$uri\fR, \f(CW$isproxy )" 4.el .IP "\f(CW$ua\fR\->get_basic_credentials( \f(CW$realm\fR, \f(CW$uri\fR, \f(CW$isproxy\fR )" 4.IX Item "$ua->get_basic_credentials( $realm, $uri, $isproxy )"This is called by \fIrequest()\fR to retrieve credentials for documentsprotected by Basic or Digest Authentication. The arguments passed inis the \f(CW$realm\fR provided by the server, the \f(CW$uri\fR requested and a booleanflag to indicate if this is authentication against a proxy server..SpThe method should return a username and password. It should return anempty list to abort the authentication resolution attempt. Subclassescan override this method to prompt the user for the information. Anexample of this can be found in \f(CW\*(C`lwp\-request\*(C'\fR program distributedwith this library..SpThe base implementation simply checks a set of pre-stored membervariables, set up with the \fIcredentials()\fR method..ie n .IP "$ua\fR\->progress( \f(CW$status\fR, \f(CW$response )" 4.el .IP "\f(CW$ua\fR\->progress( \f(CW$status\fR, \f(CW$response\fR )" 4.IX Item "$ua->progress( $status, $response )"This is called frequently as the response is received regardless ofhow the content is processed. The method is called with \f(CW$status\fR\&\*(L"begin\*(R" at the start of processing the request and with \f(CW$state\fR \*(L"end\*(R"before the request method returns. In between these \f(CW$status\fR will bethe fraction of the response currently received or the string \*(L"tick\*(R"if the fraction can't be calculated..SH "SEE ALSO".IX Header "SEE ALSO"See \s-1LWP\s0 for a complete overview of libwww\-perl5. See lwpcookand the scripts \fIlwp-request\fR and \fIlwp-download\fR for examples ofusage..PPSee HTTP::Request and HTTP::Response for a description of themessage objects dispatched and received. See HTTP::Request::Commonand HTML::Form for other ways to build request objects..PPSee WWW::Mechanize and WWW::Search for examples of morespecialized user agents based on \f(CW\*(C`LWP::UserAgent\*(C'\fR..SH "COPYRIGHT".IX Header "COPYRIGHT"Copyright 1995\-2004 Gisle Aas..PPThis library is free software; you can redistribute it and/ormodify it under the same terms as Perl itself.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -