perl LWP:UserAgent用法
(2012-02-23 11:08:27)
转载�标签: 杂谈 |
LWP::UserAgent
用法:
LWP::UserAgent是一个模拟用户浏览器的类,在使用的时候得首先创建一个LWP::UserAgent的对象,然后再设置这个对象的相关参数,它然后再创建http::Request实例,并发送请求,并返回http::Response对象。
1.创建LWP::UserAgent对象
$ua = LWP::UserAgent->new( %options )
options的键值如下:
另外,如果env_proxy的值设为真,那么代理设置将有效(参见env_proxy());如果keep_alive为真,那么LWP::ConnCache将建立(参见conn_cache())。
$ua->clone
2.LWP::UserAgent对象属性
2.1 $ua->agent
$ua->agent( $product_ID )
用来返回或者设置用户的agent,用来在header中告诉服务器你用的是什么"浏览器",设置文件头的User-Agent。缺省值是 _agent()返回的字符串。如果$product_ID以空格结尾,那么_agent()的返回值将加到$product_ID后面。user-agent必须是以/分割的浏览器名+版本号,如:
$ua->_agent返回缺省的agent值,形如"libwww-perl/#.##"
2.2 $ua->from
$ua->from( $email_address )
返回或者设置发起请求的人的邮件地址,设置文件头的from。如:
$ua->from('[email protected]');
默认设置是不发送from键值
2.3 $ua->cookie_jar
$ua->cookie_jar( $cookie_jar_obj )
返回或者设置cookie,在运行过程中必须执行两个方法,extract_cookies($request) 和 add_cookie_header($response)。在运行的过程中实际用到了http::cookies模块。如:
等价于
2.4 $ua->default_headers
$ua->default_headers( $headers_obj )
设置或返回每一次请求的headers值,缺省是一个空的http::headers 对象
$ua->default_header( $fIEld )
$ua->default_header( $fIEld => $value )
如:
my %headers=('Accept'=>'image/gif,image/jpeg,image/pjpeg,application/x-shockwave-flash,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,application/QVOD,*/*',
'Accept-Language'=>'zh-cn',
'User-Agent'=>'Mozilla/4.0 (compatible; MSIE 7.0; windows NT 5.1; TrIDent/4.0; .NET CLR 2.0.50727)',
'Accept-Charset' => 'iso-8859-1,*,utf-8');
my $response = $browser->get($url,%headers);
2.5 $ua->conn_cache
$ua->conn_cache( $cache_obj )
设置或返回LWP::ConnCache 对象
2.6 $ua->credentials( $netloc,$realm,$uname,$pass )
设置访问一个域的时候的用户名和密码
2.7 $ua->max_size
$ua->max_size( $bytes )
设置或返回响应内容的大小。缺省是undef,意思是不限制。
2.8 $ua->max_redirect
$ua->max_redirect( $n )
设置或返回被请求页面所跳转的最大次数。默认为7
2.9 $ua->parse_head
$ua->parse_head( $boolean )
设置或返回是否我们初始化响应的HTML的<head></head>标签内容。默认是TRUE,不要将这个值关闭,除非你知道你在做什么。
2.10 $ua->protocols_allowed
$ua->protocols_allowed( \@protocols )
设置或返回发起请求的方法,方法名对大小写敏感。例如$ua->protocols_allowed( [ 'http','https'] ); 表明该用户只允许这两种协议。如果用其他的协议访问URL(like "ftp://...")将会导致500错误
删除这个设置的方法: $ua->protocols_allowed(undef)
2.11 $ua->protocols_forbIDden
$ua->protocols_forbIDden( \@protocols )
与2.10类似
2.12 $ua->requests_redirectable
$ua->requests_redirectable( \@requests )
push @{ $ua->requests_redirectable },'POST';告诉LWP在POST请求发送后如果发生重新定向就自动跟随
2.13 $ua->timeout
$ua->timeout( $secs )
设置缓冲时间,默认180s
3.代理属性
$ua->proxy(\@schemes,$proxy_url)
$ua->proxy($scheme,$proxy_url)
如
指明通过制定的代理服务器,按照指定的协议方法访问。
$ua->env_proxy从*_proxy 环境变量获取代理设置,如:
4.请求方法
4.1 $ua->get( $url )
$ua->get( $url,$fIEld_name => $value,... )
如:$response=$ua->get('http://search.cpan.org/',':content_file'=>'/tmp/sco.HTML')
4.2 $ua->head( $url )
$ua->head( $url,... )
4.3 $ua->post( $url,\%form )
$ua->post( $url,\@form )
$ua->post( $url,\%form,... )
如:$response = $browser->post( $url,[formkey1 => value1,formkey2 => value2,...],headerkey1 => value1,headerkey2 => value2,);
可以用来提交搜索等等,总之就是构造网址。
4.4 $ua->mirror( $url,$filename ) 获取$url指定的文件,并按照$filename保存下来。
4.5 $ua->request( $request )
$ua->request( $request,$content_file )
$ua->request( $request,$content_cb )
$ua->request( $request,$content_cb,$read_size_hint )
4.6 $ua->simple_request( $request )
$ua->simple_request( $request,$content_file )
$ua->simple_request( $request,$content_cb )
$ua->simple_request( $request,30); ">4.7 $ua->is_protocol_supported( $scheme )
转载于:http://blog.sina.com.cn/s/blog_4df3f98901010tkj.html
总结以上是内存溢出为你收集整理的perl LWP:UserAgent用法全部内容,希望文章能够帮你解决perl LWP:UserAgent用法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)