D
Dianne van Dulken
Hi,
I'm having some trouble connecting to a third site via both SSleay and
UserAgent. I think it might have something to do with the
certificates, but I can't work out exactly what. I keep on getting a
500 error, when I am expecting a Location moved response.
Would someone mind having a quick look over my code and seeing if
there is something blatently obviously wrong that I'm missing.
Thank you very much
Dianne
!/usr/bin/perl
use strict;
use Net::SSLeay;
use LWP:
ebug qw(+);
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
$ENV{HTTPS_DEBUG} = 1;
$ENV{HTTPS_VERSION} = 3;
$Net::SSLeay::trace = 2;
$ENV{HTTPS_CERT_FILE} = 'certs/oilolig.pem';
$ENV{HTTPS_KEY_FILE} = 'certs/oilolig.pem';
sub connectto {
my ($thisurl, $method) = @_;
print "CONNECTING to $thisurl via $method \n";
my $location;
my $cookie;
if (($method eq 'post') ||($method eq 'get')) {
my ($page, $response, @headers);
my ($url1, $sub_url1) = &spliturls($thisurl);
print "split urls are $url1 and $sub_url1 \n";
if ($method eq 'post') {
print "attempting to connect via post \n";
($page, $response, @headers) =
Net::SSLeay:
ost_https($url1, 443, $sub_url1, '');
}
if ($method eq 'get') {
print "attempting to connect via get \n";
($page, $response, @headers) =
Net::SSLeay::get_https($url1, 443, $sub_url1, '');
}
for (my $i = 0; $i < $#headers; $i+=2) {
print "header $i : $headers[$i] = " . $headers[$i+1] . "\n";
if ($headers[$i] eq "LOCATION") { $location = $headers[$i+1];}
if ($headers[$i] eq "COOKIE") {$cookie = $headers[$i+1];}
}
return ($location, $cookie);
} else {
# otherwise, we want to connect via useragent
print "UA connect \n\n";
my $ua = LWP::UserAgent -> new();
my $cookie_jar = HTTP::Cookies->new(
file => "lwp_cookies.dat",
autosave => 1,
);
$ua->cookie_jar( $cookie_jar );
my $request = HTTP::Request->new(GET => $thisurl);
my $res = $ua->request($request);
my $location = $res->header('LOCATION');
my $cookie = $res->header('COOKIE');
print "location is $location \n cookie is $cookie \n";
if ($res->is_success) {
print "Connection to UA is ok\n";
}
else {
print "Connection to UA gives " . $res->status_line . "\n";
}
return "res is $res \n";
}
}
sub spliturls {
my ($url1) = @_;
print "SPLITURLS url1 is $url1 \n";
my ($junk, $url2, $sub_url2);
($junk, $url2) = split('//', $url1);
($url2, $sub_url2) = split('/', $url2, 2);
return ($url2, '/'.$sub_url2);
}
my $url1 = 'https://shopfront3.telstra.com.au/LOLOSTGCb2b/services/ProductCreateImmService';
my ($url2, $cookie) = &connectto($url1, 'post');
print "url2 : $url2 \n";
my ($url3, $cookie2) = &connectto($url2, 'ua');
print "url3 : $url3 \n";
I'm having some trouble connecting to a third site via both SSleay and
UserAgent. I think it might have something to do with the
certificates, but I can't work out exactly what. I keep on getting a
500 error, when I am expecting a Location moved response.
Would someone mind having a quick look over my code and seeing if
there is something blatently obviously wrong that I'm missing.
Thank you very much
Dianne
!/usr/bin/perl
use strict;
use Net::SSLeay;
use LWP:
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
$ENV{HTTPS_DEBUG} = 1;
$ENV{HTTPS_VERSION} = 3;
$Net::SSLeay::trace = 2;
$ENV{HTTPS_CERT_FILE} = 'certs/oilolig.pem';
$ENV{HTTPS_KEY_FILE} = 'certs/oilolig.pem';
sub connectto {
my ($thisurl, $method) = @_;
print "CONNECTING to $thisurl via $method \n";
my $location;
my $cookie;
if (($method eq 'post') ||($method eq 'get')) {
my ($page, $response, @headers);
my ($url1, $sub_url1) = &spliturls($thisurl);
print "split urls are $url1 and $sub_url1 \n";
if ($method eq 'post') {
print "attempting to connect via post \n";
($page, $response, @headers) =
Net::SSLeay:
}
if ($method eq 'get') {
print "attempting to connect via get \n";
($page, $response, @headers) =
Net::SSLeay::get_https($url1, 443, $sub_url1, '');
}
for (my $i = 0; $i < $#headers; $i+=2) {
print "header $i : $headers[$i] = " . $headers[$i+1] . "\n";
if ($headers[$i] eq "LOCATION") { $location = $headers[$i+1];}
if ($headers[$i] eq "COOKIE") {$cookie = $headers[$i+1];}
}
return ($location, $cookie);
} else {
# otherwise, we want to connect via useragent
print "UA connect \n\n";
my $ua = LWP::UserAgent -> new();
my $cookie_jar = HTTP::Cookies->new(
file => "lwp_cookies.dat",
autosave => 1,
);
$ua->cookie_jar( $cookie_jar );
my $request = HTTP::Request->new(GET => $thisurl);
my $res = $ua->request($request);
my $location = $res->header('LOCATION');
my $cookie = $res->header('COOKIE');
print "location is $location \n cookie is $cookie \n";
if ($res->is_success) {
print "Connection to UA is ok\n";
}
else {
print "Connection to UA gives " . $res->status_line . "\n";
}
return "res is $res \n";
}
}
sub spliturls {
my ($url1) = @_;
print "SPLITURLS url1 is $url1 \n";
my ($junk, $url2, $sub_url2);
($junk, $url2) = split('//', $url1);
($url2, $sub_url2) = split('/', $url2, 2);
return ($url2, '/'.$sub_url2);
}
my $url1 = 'https://shopfront3.telstra.com.au/LOLOSTGCb2b/services/ProductCreateImmService';
my ($url2, $cookie) = &connectto($url1, 'post');
print "url2 : $url2 \n";
my ($url3, $cookie2) = &connectto($url2, 'ua');
print "url3 : $url3 \n";