来源地址: http://bbs.chinaunix.net/thread-1805600-1-1.html
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响应的完整内容?
print $res->as_string;
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重定向?
$ua->max_redirect(0);
参数表示跟随重定向的层数,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');