How to install User-Agent with LWP?

I have a Perl and LWP book, but how to set the user-agent string?

This is what I have:

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 , how am I here?

+6
source share
2 answers

From the cookbook LWP :

  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); 
+9
source

I appreciate the LWP cookbook solution, which mentions a subclass solution with passing a link to lwp-request.

the wise pearl monk once said: ole subclasses of the LWP subclass :: UserAgent tag

 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 record has been revised with some bad humor, a usage statement, an _agent override, and an updated agent print line.

Bonus material for those interested: HTTP basic auth with overriding get_basic_credentials, as most people come up with a solution for the subclass. Methods are sacred or something like that; but it scratches the itch, doesn't it?

+1
source

Source: https://habr.com/ru/post/889735/


All Articles