?
http://bbs.chinaunix.net/thread-1805600-1-1.html
LWP中 文FAQ
若有变动或补充仙子会即时更新
1. 如何快速GET一个页面?
- ??use LWP::Simple;
- ??my $doc = get 'http://www.example.com/';
复制代码
2. 标准的HTTP请求过程?
- ??use LWP::UserAgent;
- ?
- ??my $ua = LWP::UserAgent->new;
- ??my $req = HTTP::Request->new(GET => 'http://www.example.com/');
- ?
- ??my $res = $ua->request($req);
- ?
- ??if ($res->is_success) {
- ? ?? ?print $res->as_string;
- ??}else {
- ? ?? ?print "Failed: ",$res->status_line,"n";
- ??}
复制代码
3. 如何得到HTTP响应状态码?
- ??print $res->status_line;
复 制代码
4. 如何得到HTTP响应的完整内容?
5. 如何得到HTTP响应的HTML解码后的内容?
- ? ?print $res->decoded_content;
复制代码
6. 如何POST数据?
- ??use LWP::UserAgent;
- ??my $ua = LWP::UserAgent->new;
- ?
- ??my $req = HTTP::Request->new(POST => 'http://www.example.com/');
- ??$req->content_type('application/x-www-form-urlencoded');
- ??$req->content('key1=value1&key2=value2');
- ?
- ??my $res = $ua->request($req);
- ??print $res->as_string;
复制代码
7. 如何接受Cookie?
- ??use LWP::UserAgent;
- ??use HTTP::Cookies;
- ?
- ??my $ua = LWP::UserAgent->new;
- ??$ua->cookie_jar(HTTP::Cookies->new(file => "lwpcookies.txt",
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? autosave => 1));
- ?
- ??my $req = HTTP::Request->new(GET => "http://www.example.com/");
- ?
- ??my $res = $ua->request($req);
- ??print $res->status_line;
复制代码
8. 如何在请求里发送指定的Cookie?
- ??use LWP::UserAgent;
- ??my $ua = LWP::UserAgent->new;
- ?
- ??my $req = HTTP::Request->new(GET => 'http://www.example.com/');
- ??$req->header('Cookie' => "key1=value1;key2=value2");
- ?
- ??my $res = $ua->request($req);
- ??print $res->status_line;
复制代码
9. 如何访问需要身份验证的网站(basic auth)?
- ??use LWP::UserAgent;
- ??my $ua = LWP::UserAgent->new;
- ?
- ??my $req = HTTP::Request->new(GET => 'http://www.example.com/');
- ??my $req->authorization_basic('user','password');
- ?
- ??my $res = $ua->request($req);
- ??print $res->as_string;
复制代码
10. 如何指定UserAgent的超时
时间
和版本?
- ??my $ua = LWP::UserAgent->new;
- ??$ua->timeout(5);
- ??$ua->agent("Mozilla/8.0");
复制代码
11. 如何让UserAgent不follow重定向?
??参数表示跟随重定向的层数,0表示不跟随重定向。
12. 如果机器有多个IP,如何指定UserAgent使用哪个IP访问
网络
?
- ??$ua->local_address('1.2.3.4');
复制代码
13. 直接使用IP访问网站,为什么会失败?
? ?那是因为没有加上Host头部,虚拟主机识别不出你的请求。
? ?在构造请求时,请加上Host头部:
- ? ?my $req = HTTP::Request->new(GET => "1.2.3.4");
- ? ? $req->header('Accept' => 'text/html',
- ? ?? ?? ?? ?? ???'Host' => 'www.example.com');
复制代码
本主题由 兰花仙子 于 2010-10-26 17:11 鉴定为 精华