Kyle R . Burton on Mon, 17 Jun 2002 14:13:54 -0400


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] capture browser request


> Kyle:
> 
> Thanks.  Netcat is installed on my system so I gave it a shot.  Worked 
> perfectly.  Now to see if I can understand LWP::UserAgent (I'm a perl head
> but not a guru).
> 
> Eric

 use strict;
 use warnings;
 use LWP::UserAgent;
 my $ua = LWP::UserAgent->new();
 my $resp = $ua->request( new HTTP::Request( 'GET', 'http://www.yahoo.com/' ) );
 print $resp->content();

It's a bit more complex if you're going to use cookies, set your own headers,
or do POSTs, but not that much more complex.  I typically wrap the user agent
with my own object to make it more convienient to use:

  package EasierUA;
  use LWP::UserAgent;
  our @ISA = qw( LWP::UserAgent );
  use HTTP::Cookies;
  use HTTP::Request;
  use HTTP::Response;
  use URI::Escape;
  
  sub new
  {
    my $self = bless LWP::UserAgent->new(), __PACKAGE__;
    $self->agent("Secret/1.0");
    $self->cookie_jar( HTTP::Cookies->new() );
    return $self;
  }
  
  sub get
  {
    my $self = shift;
    my $req = HTTP::Request->new( 'GET', @_ );
    return $self->request( $req );
  }
  
  sub post
  {
    my($self,$url,%params) = @_;
    my $req = HTTP::Request->new( 'POST', $url );
    foreach my $p (keys %params) {
      $req->add_content( uri_escape($p)."=".uri_escape($params{$p}||'') );
    }
    return $self->request( $req );
  }
  
  1;
  ##############################################################################
  package main;
  use strict;
  use warnings;
  my $ua = EasierUA->new();
  my $resp = $ua->get( 'http://www.yahoo.com/' );
  print $resp->content();
  

(yes, I did run this code before I cut and pasted it into mutt, so it
does work)


HTH

Kyle

-- 

------------------------------------------------------------------------------
Wisdom and Compassion are inseparable.
        -- Christmas Humphreys
mortis@voicenet.com                            http://www.voicenet.com/~mortis
------------------------------------------------------------------------------

______________________________________________________________________
Philadelphia Linux Users Group       -      http://www.phillylinux.org
Announcements-http://lists.phillylinux.org/mail/listinfo/plug-announce
General Discussion  -  http://lists.phillylinux.org/mail/listinfo/plug