Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

HTTP::Headers, HTTP::Request, Authentication, LWP::Useragent

by heyman (Novice)
on Mar 28, 2001 at 04:18 UTC ( [id://67692]=perlquestion: print w/replies, xml ) Need Help??

heyman has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way the "Authenticate" a session with a web site when using HTTP::Headers and HTTP::Request? EX.\

my $headers = new HTTP:Headers( Referer => "http://www.perlmonks.org/" ); my $request = new HTTP::Request( "POST", "http://www.excite.com/cgi-bin/usr44/mystuff.cgi", $headers ); my $ua = LWP::UserAgent->new; my $response = $ua->request($request); print $response->content;
I need to authenticate this session within my browser so that I may access this $request page without using my script.

ADAM

QUOTE:
Why Not!

Replies are listed 'Best First'.
Re: HTTP::Headers, HTTP::Request, Authentication, LWP::Useragent
by Daddio (Chaplain) on Mar 28, 2001 at 07:25 UTC
    I don't know if I am understanding completely, but it sounds like you need to provide a username and password for a site? If that is the case, LWP::UserAgent has the authorization_basic method that can be used something like this (directly from the lwpcook manpage):

    Documents protected by basic authorization can easily be accessed like this:
    use LWP::UserAgent; $ua = LWP::UserAgent->new; $req = HTTP::Request->new(GET => 'http://www.asite.com/'); $req->authorization_basic('myuid', 'mypassword'); print $ua->request($req)->as_string;

    The other alternative is to provide a subclass of LWP::UserAgent that overrides the get_basic_credentials() method. Study the lwp-request program for an example of this.

    Hope that helps.
      For one-off scripts it usually is really nice to use the fact that URLs have an optional "name:password@" between the protocol and the domain name. So you can write a URL like this:
      http://your_name:your_password@www.nifty.com/some/page.html

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://67692]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-26 08:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found