Hello Everyone,
I have a tricky (for me atleast) problem
1. I have to login to a website which is secured (https) using userid and password via BASIC authentication
2. I have to get the session from the login page and use that session to call one more page to upoad a file since the login page requires authentication and it redirects to login page.
3. The upload page returns a text file which i have to download? and read the conents and do some data processing.
LoginPage: https://mysite.com/login.do
UploadPage: https://mysite.com/fileupload.do
I have been trying using the LWP but with no luck. Also i am behind a proxy server and whenever i try to execute my script, I always get the following:
LWP::UserAgent::request: ()
LWP::UserAgent::simple_request: POST https://mysite.com/login.do
LWP::UserAgent::_need_proxy: Proxied to http://user:pwd@proxy_server:port
HTTP::Cookies::add_cookie_header: Checking mysite.com for cookies
HTTP::Cookies::add_cookie_header: Checking .com for cookies
LWP::Protocol::http::request: ()
LWP::Protocol::http::request: POST https://mysite.com/login.do HTTP/1.0
Host: mysite.com
Proxy-Authorization: Basic h2DrWXxyOcthqnRoaUz=
User-Agent: libwww-perl/5.53
Content-Type: application/x-www-form-urlencoded
LWP::Protocol::http::request: reading response
LWP::Protocol::http::request: HTTP/1.0 200 OK
Content-Type: text/html
Refresh: 0; URL=https://mysite.com/login.do
<HTML></HTML>
LWP::Protocol::http::request: HTTP/1.0 200 OK
LWP::Protocol::collect: read 15 bytes
LWP::UserAgent::request: Simple response: OK
I dont see any error but always the 200 OK message as seen above
Below is the code module
#!/usr/bin/perl
use warnings;
use strict;
use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Request::Common qw(POST);
use LWP::Debug qw(+);
my $upload_url = 'https://mysite.com/fileupload.do';
my $login_url = 'https://mysite.com/login.do';
&postHTTPS();
sub postHTTPS
{
my $cookie_jar = HTTP::Cookies->new; #HTTP::Cookies->new(autosa
+ve => 1)
$cookie_jar->clear;
my $action = POST ($login_url);
my $ua = new LWP::UserAgent(keep_alive=>1,env_proxy => 0);
$ua->proxy(['http','https'] => 'http://user:pwd@proxy_server:port'
+);
$ua->env_proxy();
$ua->cookie_jar($cookie_jar);
my $request = new HTTP::Request ("GET" => $login_url );
my $response = $ua->request($action);
if ($response->is_error())
{
printf " %s\n", $response->status_line;
print "https request error!\n";
}else {
my $content = $response->content();
}
if ( $response->is_success ) {
# print $response->as_string;
}else {
#print $response->status_line;
print "\nFailure!\n";
}
}
The above code is only to login. I am not sure how to get the session cookie from that login page and pass to the download page (code not written yet)
I had also asked similar question at http://www.perlmonks.com/index.pl?node_id=645153 i got some responses but still I have a problem
Is this scenario achievable in PERL? I am now learning this LWP and related modules since i am new to these
Can you one help me in this regard?
Thanks a lot in advance
Regards,
Karthik
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.