converting Java code to Perl (using LWP?)

P

PugetSoundSylvia

Hello all,

I've been banging my head against the wall trying to get some code
working in Perl. I'm assuming it will need to use the LWP module, and
I've gotten some basic things working with LWP, but not the whole
thing.

I have samples in Java, shown below. I'm stuck at the
setRequestProperty. What's the equivalent in Perl, using LWP?

I would SO appreciate any pointers on this on.

Sylvia

public TestLogin() throws IOException {
if (Debug) System.out.println("TestLogin: Logging in...");
/** Send the HTTPS request */
URL url_login = new URL(Profile.Protocol+"://"+ /** i.e,
either http or https */
Profile.Hostname+"/api/login.php"+ /** the
actual website, like TestSite.org */
"?name="+Profile.UserID+ /** site
specific parameter */
"&pass="+Profile.Password+ /** site
specific parameter */
"&service_type="+Profile.ServiceType); /** site
specific parameter */
URLConnection urlcon_login = url_login.openConnection();
String auth = Profile.FWUser+":"+Profile.FWPassword;
urlcon_login.setRequestProperty("Authorization", "Basic "+
Base64.encode(auth.getBytes()));
/** Get the HTML response and parse it */
BufferedReader inbuf_login;
inbuf_login = new BufferedReader(
new InputStreamReader(urlcon_login.getInputStream()));
if (inbuf_login != null) parse(inbuf_login);
inbuf_login.close();
if (Debug) System.out.println("TestLogin: Login completed
(Session ID = " + Profile.SessionID+")");
}
 
B

Ben Morrow

Quoth (e-mail address removed):
Hello all,

I've been banging my head against the wall trying to get some code
working in Perl. I'm assuming it will need to use the LWP module, and
I've gotten some basic things working with LWP, but not the whole
thing.

I have samples in Java, shown below. I'm stuck at the
setRequestProperty. What's the equivalent in Perl, using LWP?

Since you haven't posted any Perl, it's rather hard to know where you're
going wrong. You can set request headers with HTTP::Request->header; you
can specify credentials to be used with HTTP auth on every request with
LWP::UserAgent->credentials.

Ben
 
P

PugetSoundSylvia

Thanks for your response, Ben. I've posted what I have so far below
(it doesn't work, returns 'Error: 401 Unauthorized').

Also, here's what my limited documentation says:

In order to be allowed through the firewall, users must send an
authentication HTTP header
along with every request.
The header structure is: Authorization: BASIC xxxxxxxxxxxxxxxx…
where xxxxxxx is a firewall user name and password separated by
“:” and converted into BASE64-
encoded format.

***************************************
***************************************

use MIME::Base64;
use LWP::UserAgent;

$ua = LWP::UserAgent->new;

$req = HTTP::Request->new(GET => 'http://SiteName.org/login.php?
name=AppName&pass=AppPWD&service_type=xxx');
$Encoded = encode_base64('FireWallName:FireWallPWD',''); #Encode
into Base64
$FullAuthorization = 'Basic ' . $Encoded; # not sure if Basic is
upper or lower - documentation is contradictory
$req->header('Authorization' => $FullAuthorization);

# send request
$res = $ua->request($req);

# check the outcome
if ($res->is_success) {
print $res->decoded_content;
}
else {
print "Error: " . $res->status_line . "\n";
}
 
B

Ben Morrow

Quoth (e-mail address removed):
Thanks for your response, Ben. I've posted what I have so far below
(it doesn't work, returns 'Error: 401 Unauthorized').

Also, here's what my limited documentation says:

In order to be allowed through the firewall, users must send an
authentication HTTP header
along with every request.
The header structure is: Authorization: BASIC xxxxxxxxxxxxxxxx…
where xxxxxxx is a firewall user name and password separated by
“:” and converted into BASE64-
encoded format.

***************************************
***************************************

use MIME::Base64;
use LWP::UserAgent;

$ua = LWP::UserAgent->new;

$req = HTTP::Request->new(GET => 'http://SiteName.org/login.php?
name=AppName&pass=AppPWD&service_type=xxx');
$Encoded = encode_base64('FireWallName:FireWallPWD',''); #Encode
into Base64
$FullAuthorization = 'Basic ' . $Encoded; # not sure if Basic is
upper or lower - documentation is contradictory

It's case-insensitive, so either ought to work.
$req->header('Authorization' => $FullAuthorization);

# send request
$res = $ua->request($req);

# check the outcome
if ($res->is_success) {
print $res->decoded_content;
}
else {
print "Error: " . $res->status_line . "\n";
}

That ought to work, I'd say. It would be easier to simply call
$ua->credentials, passing the appropriate information, and let LWP
handle constructing the header for you; finding out what the correct
'netloc' and 'realm' are will mean printing out the contents of the 401
response.

If this doesn't work, add

use LWP::Debug qw/+conns/;

to the top of your script, and post the output.

Ben
 
P

PugetSoundSylvia

Hi Ben - thanks for the response. I finally figured it out -
embarassingly enough, I wasn't sending the userid/password through
correctly, I had switched the one with the other. I finally figured
it out when I un-encoded the encoded stuff, and there it was, plain as
day.

Sylvia

Quoth (e-mail address removed):




Thanks for your response, Ben.  I've posted what I have so far below
(it doesn't work, returns 'Error: 401 Unauthorized').
Also, here's what my limited documentation says:
   In order to be allowed through the firewall, users must send an
authentication HTTP header
   along with every request.
   The header structure is:   Authorization: BASIC xxxxxxxxxxxxxxxx…
   where  xxxxxxx is a firewall user name and password separated by
“:” and converted into BASE64-
   encoded format.

use MIME::Base64;
useLWP::UserAgent;
$ua =LWP::UserAgent->new;
$req = HTTP::Request->new(GET => 'http://SiteName.org/login.php?
name=AppName&pass=AppPWD&service_type=xxx');
$Encoded = encode_base64('FireWallName:FireWallPWD','');   #Encode
into Base64
$FullAuthorization = 'Basic ' . $Encoded;       # not sure if Basic is
upper or lower - documentation is contradictory

It's case-insensitive, so either ought to work.
  $req->header('Authorization' => $FullAuthorization);
# send request
$res = $ua->request($req);
# check the outcome
if ($res->is_success) {
  print $res->decoded_content;
}
else {
  print "Error: " . $res->status_line . "\n";
}

That ought to work, I'd say. It would be easier to simply call
$ua->credentials, passing the appropriate information, and letLWP
handle constructing the header for you; finding out what the correct
'netloc' and 'realm' are will mean printing out the contents of the 401
response.

If this doesn't work, add

    useLWP::Debug qw/+conns/;

to the top of your script, and post the output.

Ben

--
Many users now operate their own computers day in and day out on various
applications without ever writing a program. Indeed, many of these users
cannot write new programs for their machines...
    -- F.P. Brooks, 'No Silver Bullet', 1987             [[email protected]]- Hide quoted text -

- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top