Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set User-Agent with LWP?

Tags:

perl

I have the Perl & LWP book, but how do I set the user-agent string?

This is what I've got:

use LWP::UserAgent;
use LWP::Simple; # Used to download files

my $u = URI->new($url);
my $response_u = LWP::UserAgent->new->get($u);
die "Error: ", $response_u->status_line unless $response_u->is_success;

Any suggestions, if I want to use LWP::UserAgent like I do here?

like image 434
Louise Avatar asked Jun 03 '11 20:06

Louise


2 Answers

From the LWP cookbook:

  use LWP::UserAgent;
  $ua = new LWP::UserAgent;
  $ua->agent("$0/0.1 " . $ua->agent);
  # $ua->agent("Mozilla/8.0") # pretend we are very capable browser

  $req = new HTTP::Request 'GET' => 'http://www.sn.no/libwww-perl';
  $req->header('Accept' => 'text/html');

  # send request
  $res = $ua->request($req);
like image 163
Jim Garrison Avatar answered Oct 13 '22 22:10

Jim Garrison


I do appreciate the LWP cookbook solution which mentions the subclassing solution with a passing reference to lwp-request.

a wise perl monk once said: the ole subclassing LWP::UserAgent trick

package AgentP;
  use base 'LWP::UserAgent';
  sub _agent       { "Mozilla/8.0" }
  sub get_basic_credentials {
      return 'admin', 'password';
  }

package main;
  use AgentP;
  my $agent    = AgentP->new;
  my $response = $agent->get( 'http://127.0.0.1/hideout.html' );

  print $agent->agent();

the entry has been revised with some poor humor, use statement, _agent override, and updated agent print line.

Bonus material for the interested: HTTP basic auth provided with get_basic_credentials override, which is how most people come to find the subclassing solution. _methods are sacred or something; but it does scratch an itch doesn't it?

like image 22
Dave Horner Avatar answered Oct 14 '22 00:10

Dave Horner