Basic Authentication Workaround?

They have: 19 posts

Joined: Oct 2000

Hi,

I've written a basic script below which normally works to grab pages but not from my protected site. Naturally I know the Realm, Username and Password but don't know how to incorporate it into my current program to get access to the pages.

Hope someone out there can help!

Denise Smiling
-----------------------------------------------------

#!/usr/bin/perl

&IOSocket;

sub IOSocket{
use IO::Socket;
$method = "GET";
my @f = split(/\//, $input);
$host = "www.domain.com";; #their domain...
$path = "/their/root/protected"; #protected root etc...
$socket = new IO::Socket::INET( PeerAddr => $host,
PeerPort => 80,
Proto => 'tcp',
Type => SOCK_STREAM, ) or die &printout;
print $socket "$method $path HTTP/1.0\nReferer: $host\n";
print $socket "User-Agent: $ENV{'HTTP_USER_AGENT'}\n\n";
@output = <$socket>;
close ($socket);
}

$FILE="d:/Inetpub/mydomain/file.htm"; #my local domain directory path

open(WRBK,">$FILE");

print WRBK "@output\n";

close(WRBK);

exit;

They have: 193 posts

Joined: Feb 2000

Example:

http://USER:[email protected]

I guess all you would need to do is change $host to reflect the above format.

Hope that helped. Smiling

Richard

[email protected]

Everyone here has a website. It's just that not all are worth posting (Mine! Smiling).

They have: 19 posts

Joined: Oct 2000

Hi Richard,

Thanks for the advice but it didn't work.

I made $host = "myusername:mypassword\@mydomain.com";
but it didn't recognize the url and died.

Any ideas?

Denise

They have: 122 posts

Joined: Jun 2000

It wouldn't, because Perl has no idea that you're trying to connect to an httpd there. I believe that LWP allows you to set Username and Password for basic authentication.

Rob Radez
OSInvestor.com

They have: 193 posts

Joined: Feb 2000

I think I agree with Rob. The LWP module should allow you to use the authentication format I gave you...as it simply calls a URL.

Richard

They have: 19 posts

Joined: Oct 2000

Hi guys,

Thanks, I looked into the LWP function and constructed a routine that certainly does grab pages and write them to a file but even though I now know the constructor that authenticates, I need some help inserting it into the routine below:

-----------------

#!/usr/bin/perl

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;

$FILE="d:/Inetpub/mydomain/import.htm";

my $ua = new LWP::UserAgent;
my $request = new HTTP::Request('GET', 'http://www.mydomain.com/index.htm');
my $response = $ua->request($request, $FILE);
if ($response->is_success) {
print $response->content;
} else {
print $response->error_as_HTML;
}

exit;

-------------------

Here is the authetnication:

$ua->credentials('www.mydomain.com', 'realm', 'username', 'password');

In the Perl help files it says that you can use $request like this:

request($request [, $subroutine]);

$subroutine being the authentication call.. but whenever I try to build the authentication call above into $subroutine it just comes up with a syntax error for the "'", or just doesn't do anything and returns 401 access denied.

Any ideas?

Cheers,

Denise Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.