perl --- LWP::UserAgent

来源:互联网 发布:淘宝怎么批量定时上架 编辑:程序博客网 时间:2024/05/20 22:31
 

  LWP::UserAgent  --- web user agent class, is a class implementing aweb user agent. LWP::UserAgent objects can be used to dispatch webrequests.


use LWP::UserAgent;
use strict;

my $url = "http://www.myspace.cn";

my $ua = LWP::UserAgent->new(env_proxy => 1,
                              keep_alive => 1,
                              timeout => 15);

  my $response = $ua->get($url);
if ($response->is_success) {
    my    $data = $response->content();

     print "$data/n";
}
    else {

          print "/nfailed!/n";
    }

 

  In normal use the application creates an LWP::UserAgent object, andthen configures it with values for timeouts, proxies, name, etc. Itthen creates an instacne of HTTP::Request for the request that needs tobe performed. This request is then passed to one of the request methodthe UserAgent, which dispachtes it using the relevant protocol, andreturns a HTTP::Response object. There are convenience methods forsending the most common request types: get(), head() and post().

【Constructor methods】

    $ua = LWP::UserAgent-> new(%options);

    This method constructs a new LWP::UserAgent object and returns it.Key/Value pair arguments may be provided to set up the initial stgate.           KEY                                        DEFAULT
    -----------                        --------------------
    agent                                    " libwww-perl/#.###"
    from                                      undef
    conn_cache                          undef
    cookie_jar                          undef
    default_headers                HTTP::Headers-> new
    max_size                              undef
    max_redirect                      7
    parse_head                          1
    protocols_allowed            undef
    protocols_forbidden        undef
    requests_redirectable    ['GET', 'HEAD']
    timeout                                180

【Attributes】

  The settings of the configuration attributes modify the behaviour ofthe LWP::UserAgent when it dispatches requests. Most of these can alsobe initialized by options passed to the constructor method.

    $ua-> agent(),....

【Request Methods】

  These methods are used to dispatch requests wia the user agent:

  (1) $ua-> get($url)

  (2) $ua-> get($url,$field_name-> $value,....)

      These two methods will dispatch a Get Requests on the given $url.

(3) $ua-> head($url)

(4)$ua-> head($url,$field_name=> $value,...)

    These two methods will dispatch a Head Request on the given $url.

(5) $ua-> post($url,/%form)

(6) $ua-> post($url,/@form)

(7) $ua-> post( $url, /%form, $field_name => $value, ... )
(8) $ua-> post( $url, $field_name => $value,... Content => /%form )
(9) $ua-> post( $url, $field_name => $value,... Content => /@form )
(10) $ua-> post( $url, $field_name => $value,... Content => $content )

These method will dispatch a POSTrequest on the given $url, with %form or @form providing the key/valuepairs for the fill-in form content. Additional headers and contentoptions are the same as for the get() method.

please see  http://search.cpan.org/~gaas/libwww-perl-5.820/lib/LWP/UserAgent.pm  for details