Sunday, October 01, 2006

upload file to https server using perl

... had some trouble initially to post / upload a file to https server
but using lwp::useragent it was easy ..

here is my code

#!/usr/bin/perl

use Data::Dumper;
use LWP::UserAgent;
use HTTP::Cookies;


my $https_login = 'https://192.168.1.3';
my $https_post = 'https://192.168.1.3/upload';


my $https_user = 'xxxxx';
my $https_pass = 'yyyyy';


&postHTTPS();


sub postHTTPS
{
#secure login


my $ua = LWP::UserAgent->new();


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


$ua->cookie_jar(HTTP::Cookies->new(file =>
".cookies.txt",autosave => 1));


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


#print Dumper($response);


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


$response = $ua->post($https_post, [
filename =>['/tmp/abc.txt'],
], );


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

2 comments:

Anonymous said...

Thanks a lot!

Unknown said...

Thanks!