converting Java code to Perl (using LWP?)

Discussion in 'Perl Misc' started by PugetSoundSylvia@gmail.com, Jun 13, 2008.

  1. Guest

    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+")");
    }
     
    , Jun 13, 2008
    #1
    1. Advertising

  2. Ben Morrow Guest

    Quoth :
    > 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

    --
    Razors pain you / Rivers are damp
    Acids stain you / And drugs cause cramp. [Dorothy Parker]
    Guns aren't lawful / Nooses give
    Gas smells awful / You might as well live.
     
    Ben Morrow, Jun 13, 2008
    #2
    1. Advertising

  3. Guest

    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";
    }
     
    , Jun 13, 2008
    #3
  4. Ben Morrow Guest

    Quoth :
    > 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

    --
    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 []
     
    Ben Morrow, Jun 14, 2008
    #4
  5. Guest

    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

    On Jun 13, 4:33 pm, Ben Morrow <> wrote:
    > Quoth :
    >
    >
    >
    >
    >
    > > 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             []- Hide quoted text -
    >
    > - Show quoted text -
     
    , Jun 23, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tommy
    Replies:
    0
    Views:
    1,041
    Tommy
    Sep 22, 2003
  2. JD Vernon

    Perl, Oracle, LWP, and PDF's

    JD Vernon, Feb 26, 2004, in forum: Perl
    Replies:
    1
    Views:
    846
  3. Vinay Gupta
    Replies:
    2
    Views:
    1,108
    Michael Schlenker
    Jul 27, 2004
  4. Replies:
    13
    Views:
    2,744
    Arne Vajhøj
    Mar 18, 2008
  5. Replies:
    2
    Views:
    422
    Martijn Lievaart
    Aug 2, 2008
Loading...

Share This Page