Chinaunix首页 | 论坛 | 博客
  • 博客访问: 391217
  • 博文数量: 117
  • 博客积分: 4416
  • 博客等级: 上校
  • 技术积分: 1135
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-08 14:16
个人简介

一生醉生忘死,就让他继续下去吧!

文章分类

全部博文(117)

分类: Python/Ruby

2011-08-09 14:57:57

1、request object 类名为HTTP::Request
    包含如下属性:
  (1)method :get,put,post,head
    (2)url
    (3)headers
    (4)content
 
2、response object 类名为HTTP::Reponse
   包含如下属性:
  (1)code :is a numerical value that indicates the overall outcome of the request
    (2)message
    (3)headers
    (4)content
    (5)is_success()=====is_error()
 
3、The user agent  类名为LWP::UserAgent
     包含如下属性:
    (1)timeout:指定你给远程服务器相应的时间
  (2)agent:指定应用程序的名字
  (3)from:制订email地址的属性
  (4)parse_head:指定初始化响应部分属性
  (5)proxy/no_proxy:指定是否有代理服务器
  (6)credentials:提供设置用户名和密码的途径
 
 
Example:
  # Create a user agent object
  use LWP::UserAgent;
  my $ua = LWP::UserAgent->new;
  $ua->agent("MyApp/0.1 ");

  # Create a request
  my $req = HTTP::Request->new(POST => '');
  $req->content_type('application/x-www-form-urlencoded');
  $req->content('query=libwww-perl&mode=dist');

  # Pass request to the user agent and get a response back
  my $res = $ua->request($req);

  # Check the outcome of the response
  if ($res->is_success) {
      print $res->content;
  }
  else {
      print $res->status_line, "\n";
  }
  
 
实例应用:
1. 如何快速GET一个页面?
  use LWP::Simple;
  my $doc = get '';
 
2.标准的HTTP请求过程?
  use LWP::UserAgent;
  my $ua = LWP::UserAgent->new;
  my $req = HTTP::Request->new(GET => '');
  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 => '');
  $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 => "");
  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 => '');
  $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 => '');
  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' => '');
 
 
阅读(1725) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~