首页 > 代码库 > LWP::UserAgent介绍2

LWP::UserAgent介绍2

 1 #这个LWP::UserAgent一般要配合其他模块使用
 2 #比如:
 3 #HTTP::Request
 4 #HTTP::Cookie
 5 #HTTP::Respose
 6 #HTTP::Status
 7 #LWP::UserAgent相当于创建一个模拟浏览器
 8  
 9 #用以下方式创一个浏览器
10 my $useragent = LWP::UserAgent->new();
11 #一般来说, 我们参数不添加, 到下面要用时再添加, 或让它使用默认值, 它的默认参数如下
12  
13 =pod
14            KEY                     DEFAULT
15            -----------             --------------------
16            agent                   "libwww-perl/#.###"
17            from                    undef
18            conn_cache              undef
19            cookie_jar              undef
20            default_headers         HTTP::Headers->new
21            local_address           undef
22            ssl_opts                { verify_hostname => 1 }
23            max_size                undef
24            max_redirect            7
25            parse_head              1
26            protocols_allowed       undef
27            protocols_forbidden     undef
28            requests_redirectable   [GET, HEAD]
29            timeout                 180
30 =cut
31  
32 如果你想添加参数, 用这种形式:
33 my $useragent = LWP::UserAgent->new(agent => PERL);
34 =pod
35 一般来说, 我们不用配置他上面的参数
36 当然, 如果你想测试一下, 可以用下面的代码打印一下默认值, 但不一定能正常打印
37 =cut
38  
39 #!/usr/bin/perl -w
40 use strict;
41 use LWP::UserAgent;
42  
43 my $ua = LWP::UserAgent->new();
44 print $ua->agent,"\n";
45 print $ua->from,"\n";
46 print $ua->conn_cache,"\n";
47 print $ua->cookie_jar,"\n";
48 print $ua->default_headers,"\n";
49 print $ua->local_address,"\n";
50 print $ua->ssl_opts,"\n";
51 print $ua->max_size,"\n";
52 print $ua->max_redirect,"\n";
53 print $ua->parse_head,"\n";
54 print $ua->protocols_allowed,"\n";
55 print $ua->protocols_forbidden,"\n";
56 print $ua->requests_redirectable,"\n";
57 print $ua->timeout,"\n";
58  
59 #可以看到有一些值是要从其他模块去获得的, 像$ua->default_headers, 要从HTTP::Headers->new去构建

 

LWP::UserAgent介绍2