Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Perl fetch URL Username and Password

Posted on 2003-11-02
6
Medium Priority
?
1,702 Views
Last Modified: 2007-12-19
Hi

I am writing a script to fetch content of a page...this page requires a user name and password .... the login page is like this

<script language="javascript">
function validate(){
      if (document.LOGINfrm.USER.value==""){
            alert("No user ID supplied !")
            return false
      }

      if (document.LOGINfrm.PSWD.value==""){
            alert("No password supplied !")
            return false
      }
      return true
}
</script>

<FORM NAME="LOGINfrm" METHOD="POST" ACTION="/LOGIN" onSubmit="return validate()">

<TD ALIGN=RIGHT><B>User id:</B>&nbsp;</TD>
<TD><INPUT NAME="USER" SIZE=20 MAXLENGTH=20></TD>
<B>Password:</B>&nbsp;</TD>
<TD><INPUT TYPE="PASSWORD" NAME="PSWD" SIZE=20 MAXLENGTH=20></TD>

<INPUT TYPE="HIDDEN" NAME="REFER" VALUE="/cgi-bin/BRW?BRW=BDN=1234">

---------------------------------------------
this my script

#!c:/perl/bin/perl

use CGI::Carp;
use CGI::Carp qw(fatalsToBrowser);
use strict "vars";
use LWP::Simple;
print "Content-type: text/html\n\n";
$url = 'http://somesite.com/cgi-bin/BRW?BRW=BDN=1234';
$script_insides = get($url);
print $script_insides;

----------------------------------------------
How do i put my username and password in the script?

i have tried this already: 'http://username:password@somesite.com/cgi-bin/BRW?BRW=BDN=1234'


Cheers
0
Comment
Question by:baz_can_fix_it
6 Comments
 
LVL 3

Expert Comment

by:cwp
ID: 9669168
See if you can get away with http://somesite.com/LOGIN?REFER=%2fcgi-bin%2fBRW%3fBRW%3dBDN%3d1234&USER=[username]&PSWD=[password]. If that doesn't work, you'll need something more complex than just stuff from LWP::Simple.
0
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 9669266
Hello... Nope no luck...I even tried using it manually without the script.... I'll have a read on LWP::Simple...I'll get back to you...


Cheers
0
 
LVL 3

Accepted Solution

by:
cwp earned 120 total points
ID: 9669348
Try this:
   use HTTP::Request::Common;
   $ua = LWP::UserAgent->new;
   $response = $ua->request(POST 'http://somesite.com/LOGIN',
      [USER => [username],
       PSWD => [password],
       REFER => '/cgi-bin/BRW?BRW=BDN=1234'
      ]
   );

   if ($response->is_success) {
       print $response->content;
   } else {
       print $response->error_as_HTML;
   }

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:jmcg
ID: 9669377
The Javascript validate routine is not a barrier in this case. It's just checking that the two values are supplied, it's not changing them in any way.

Here's a script I've been using on Experts-Exchange. It solves nearly the same problem that you have with your somesite.com login form. I'd change it for you, but then I wouldn't be able to test it.

================
#! /usr/bin/env perl

use LWP;
use HTTP::Cookies;
use HTTP::Request::Common;

my $url = "http://www.experts-exchange.com/";

my $ua = LWP::UserAgent->new;
$cookie_jar = HTTP::Cookies->new( file=>'cookies.txt',
        autosave=>1);
$ua->cookie_jar($cookie_jar);
$ua->agent( 'Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Creative)');
push @{ $ua->requests_redirectable }, 'POST';

# establish session
my $req = POST $url . 'login.jsp' ,
   [
     redirectURL=>"/Programming/Programming_Languages/Perl/",
     msuLoginName=>'jmcg',
     msuPassword=>'put-a-real-password-here',
     msuRememberMe=>'on',
     msuLoginSubmit.x=>0,
     msuLoginSubmit.y=>0,
   ];

my $res = $ua->request( $req);

##      print "===== Request Headers =====\n";
##      print $req->headers->as_string;
##      print "===== Response Headers ====\n";
##      print $res->headers->as_string;
##      print "===== Response Content ====\n";
##      print $res->content;

# turn Expert mode on
my $req = GET $url . 'toggleExpertMode.jsp' .
     "?toggle=on" .
     "&redirectURL=/Programming/Programming_Languages/Perl/"
     ;

my $res = $ua->request( $req);

##      print "===== Request Headers =====\n";
##      print $req->headers->as_string;
##      print "===== Response Headers ====\n";
##      print $res->headers->as_string;
##      print "===== Response Content ====\n";
##      print $res->content;


while( <> ) {
    next unless /(Q_\d+)/;
    my ($qid) = $1;

    print STDERR "fetching $qid ...";
    my $req = GET ($url . $qid . '.html');
    my $res = $ua->request( $req );

    print STDERR " saving ...";
    if( $res->is_success) {
        open QID, ">../../ee_data/QID/$qid.html" or
          die "could not open QID file $qid: $!";
        print QID $res->content;
        close QID;
      } else {

      print 'Error: ', $res->status_line, "\n";
      print "===== Request Headers =====\n";
      print $req->headers->as_string;
      print "===== Response Headers ====\n";
      print $res->headers->as_string;
      print "===== Response Content ====\n";
      print $res->content;
      exit 1;
      }

    print STDERR "\n";
    sleep 2;
  }

0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 80 total points
ID: 9669379
http://somesite.com/LOGIN?USER=username&PSWD=base64-encoded-password

You can retrieve the values with CGI, like:
  use CGI; my $q new CGI;
  my $user = $q->param('USER');
0
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 9675440
Hi cwp & ahoffmann together you make an good answer...so I'll increase the points and split....

Sorry jmcg I am sure yours probably would of worked...but too much modifications for my needs...

Cheers

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question