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

<title>Here is my code and debug run</title> </head> <body>

Here is my code and debug run. This was working previously, and I cannot get the powers that be to admit that they changed anything. Can anyone tell me what I need to add to this code to make it work again, please???
<code><c></code>
use LWP::Debug qw(+);
use HTML::TokeParser;
use URI;
use Date::Simple (':all');
use LWP::UserAgent;
#use Term::ReadKey;
use LWP 5.64;
use Crypt::SSLeay;
my $browser;

my $realm ='Engineering Private Directory (use your unix p/w)';
my $server = '$URL';
my $port = '80';

my $username = "$username";
my $password = "$passwd";
chomp ($password);
$browser=LWP::UserAgent->new;

$browser->env_proxy;
# Now for the **magic**...
$browser->credentials(
'$url:80',
'Engineering Private Directory \(use your unix p/w\)',
'login' => 'passwd'
);

my $url="$url";
my $req = $browser->get($url);
$req->authorization_basic($username, $password);
my $res = $browser->request($req);
print $res->as_string;

my $response = $browser->get($url);
print "Error: ", $response->header('WWW-Authenticate') ||
'Error accessing',
# ('WWW-Authenticate' is the realm-name)
"\n ", $response->status_line, "\n at $url\n Aborting"
unless $response->is_success;
exit;
<code><c></code>
**************************************************************

Debug Output:
LWP::UserAgent::new: ()
LWP::UserAgent::request: ()
LWP::UserAgent::send_request: GET $URL
LWP::UserAgent::_need_proxy: Not proxied
LWP::Protocol::http::request: ()
LWP::Protocol::http::request: GET / HTTP/1.0
Host: $HOST
User-Agent: libwww-perl/5.803

LWP::Protocol::http::request: reading response
LWP::Protocol::http::request: HTTP/1.1 302 Found
Date: Fri, 16 May 2008 14:10:34 GMT
Server: Apache/2.2.8 (Unix) mod_ssl/2.2.8 OpenSSL/0.9.8e DAV/2 PHP/5.2.6
Location: $URL
Content-Length: 335
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="$URL">here</a>.</p>
<hr>
<address>Apache/2.2.8 (Unix) mod_ssl/2.2.8 OpenSSL/0.9.8e DAV/2 PHP/5.2.6 Server at $URL Port 80</address>
</body></html>
LWP::Protocol::http::request: HTTP/1.1 302 Found
LWP::Protocol::collect: read 335 bytes
LWP::UserAgent::request: Simple response: Found
LWP::UserAgent::request: ()
LWP::UserAgent::send_request: GET $URL
LWP::UserAgent::_need_proxy: Not proxied
LWP::Protocol::http::request: ()
LWP::Protocol::http::request: GET / HTTP/1.0
Host: $HOST
User-Agent: libwww-perl/5.803

LWP::Protocol::http::request: reading response
LWP::Protocol::http::request: HTTP/1.1 401 Authorization Required
Date: Fri, 16 May 2008 14:10:37 GMT
Server: Apache/2.2.8 (Unix) mod_ssl/2.2.8 OpenSSL/0.9.8e DAV/2 PHP/5.2.6
WWW-Authenticate: Basic realm="Engineering Private Directory (use your unix p/w)"
Accept-Ranges: bytes
Content-Length: 2968
Connection: close
Content-Type: text/html

LWP::Protocol::http::request: HTTP/1.1 401 Authorization Required
LWP::Protocol::collect: read 2968 bytes
LWP::UserAgent::request: Simple response: Unauthorized
LWP::UserAgent::request: ()
You need a request object, not a HTTP::Response object at trygnats.pl line 48

</body> </html>
  • Comment on LWP::Credentials help with previously working perl script PLEASE???

Replies are listed 'Best First'.
Re: LWP::Credentials help with previously working perl script PLEASE???
by pileofrogs (Priest) on May 16, 2008 at 16:24 UTC
    my $server = '$URL';

    You're using single quotes, so the value of $server is literally $URL, not the contents of $URL. Use double quotes and it will expand $URL.

    --Pileofrogs

    P.S. Can you please clean up the front-page garbage in your post?

      I am actually hard coding the info, I just replaced it with $URL and the like to hide it from your eyes. So, the quotes don't matter in this case. thanks.
Re: LWP::Credentials help with previously working perl script PLEASE???
by tachyon-II (Chaplain) on May 17, 2008 at 00:29 UTC

    This code works.

    use LWP::UserAgent; my $url = "http://prism.library.cornell.edu/control/authBasic/authTest +/"; my ($servername) = $url =~ m!://([^/]+)!; my $port = 80; my $realm = 'User: test Pass:'; my $username = 'test'; my $password = 'this'; my $lwp = LWP::UserAgent->new( ); $lwp->cookie_jar( {} ); $lwp->credentials("$servername:$port", $realm, $username, $password); my $response = $lwp->get($url); print $response->as_string;

    In your code you have:

    my $url="$url"; my $req = $browser->get($url); <--- response object, not request obje +ct $req->authorization_basic($username, $password); <--- here is where yo +u pass your response object <--- generating the e +rror..... my $res = $browser->request($req); print $res->as_string;
      Thank-you tachyon-II. I modified the code as you suggested. I am seeing a bit more debug which leads me to believe this is an ssl issue. Client-SSL-Warning: Peer certificate not verified Title: Access Denied Do you know how to code this so it works with ssl please?

        Different problem. In your example you are connecting to port 80 or vanilla HTTP. Now you are coming up with an SSL error. Perhaps the site is offering up an invalid certificate. Maybe you should be connecting to port 443. I have given you a perfectly functional example. I am not a mind reader, and have no intention to debug invisible code accessing an equally invisible site which is throwing an error that seems unlikely to be related to the code you have posted.