troubleshooting Question

PERL script to upload a file to https site

Avatar of pavelmed
pavelmed asked on
PerlSSL / HTTPS
4 Comments1 Solution964 ViewsLast Modified:
I have to create a script to automate uploading a file to a secure https site.

I have the URL to login to the site ('https://servername.com/login/home.do#login'), username and password.
While logged in, the site displays an option to select a directory to upload to, a browser button to select a file, and a button upload.

I am trying to automate it using a PERL script but my attempts have been unsuccessful.

Do you know good examples of scripts doing that?
If yes, could you please provide them?


I tried to use a couple of samples I could find, and my code is below
Do you have any suggestions on what should be fixed?

Thank you.

When I ran the script with simply a servername in a https address ('https://servername.com/login/home.do#login'), it returned an error
"500 Can't connect to servername.com:443 (certificate verify failed)"

Then, when I noticed that I was also given a port number (say, 5555), I replaced the URL
with 'https://servername.com:5555/login/home.do#login'
This time the error was different and came after about 30 sec of wait:
500 Can't connect to servername.com:5555 (A connection attempt failed because
 the connected party did not properly respond after a period of time,
or established connection failed because connected host has failed to respond.)

Please note that the port number should NOT be used while calling the login page in the Internet browser.

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

# Use HTTP Web Client Library (LWP)
use LWP::UserAgent;
use HTTP::Request;

#from the sample
use HTTP::Cookies;


my $https_login = 'https://servername.com/login/home.do#login';
my $https_post = 'https://servername.com/login/home.do#myFilegateway/Inbox';

my $https_user = 'myUserName';
my $https_pass = 'myPassword';

# create instance of user agent (web browser) session
my $browser = LWP::UserAgent->new();

# need to create temporary "browser" cookie for session state persistence
# cookie stores the session ID used by websites
$browser->cookie_jar( {} );

# Please note: I was also trying to use the following line with the same result:
#$browser->cookie_jar(HTTP::Cookies->new(file => ".cookies.txt",autosave => 1));


$browser->protocols_allowed( [ 'https'] );

# Please note: I tried to use each one of the statements below, sending first "user", then trying "userName" (which is a name of
# the UserName control on the web page

#my $response = $browser->post($https_login, [ 'userName' => "$https_user",'password' => "$https_pass" ] );
my $response = $browser->post($https_login, [ 'user' => "$https_user",'password' => "$https_pass" ] );


if ($response->is_error())
{
printf " %s\n", $response->status_line;
print "https request error!\n";
} else {
my $content = $response->content();
print "$content\n";
}


$response = $browser->post($https_post, [filename => ['./theFileNametoUpload.txt'],], );


if ($response->is_error())
{
printf " %s\n", $response->status_line;
print "https request error!\n";
} else {
my $content = $response->content();
print "$content\n";
}
ASKER CERTIFIED SOLUTION
nociSoftware Engineer
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros