HTTPSClient
- HTTPS Client to connect to a HTTPS-Server and execute POST- and GET- methods.
use HTTPSClient;
# instantiate
my $c=HTTPSClient->new (Host=>"localhost:2000",
SSL_verifycn_name => "server.domain.name.no",
SSL_verifycn_scheme => "https",
SSL_key_file => "./private.key",
SSL_cert_file => "./public.key",
SSL_ca_file => "./DigiCertCA.crt",
);
# attempt connect
if (!$c->connect()) {
print "Error! Unable to connect: ".$c->error()."\n";
}
# execute a POST method
my %resp;
if (!$c->lazydo ("/whatEverServerMethod",\%resp,methodpar1=>"blipp",methodpar2=>"blapp",methodparN=>"whatever")) {
print "Error! Unable to execute method: ".$c->error();
} else {
use Data::Dumper;
print "RESULT: ".Dumper(\%resp);
}
HTTPS Client class to connect to a HTTPS-server and execute POST- and GET- methods. It takes a Content-class as the parameter upon instantiation and uses that to convert the parameters into the defined Content-class type for the server.
Upon a response the module then decode the return using the same Content-class type before returning the response as a HASH to the caller.
Instantiates the class.
Accepts the following parameters:
Host Hostname of the HTTPS-server to connect to, including the port. Specified in the format: HOSTNAME:PORT.
SSL_hostname Same as Host-parameter. See Net::HTTPS for documentation.
SSL_key_file Sets the private key to use by the client. Includes full path and filename. Defaults to undef. See Net::HTTPS for documentation.
SSL_cert_file Sets the public key to use by the client. Includes full path and filename. Defaults to undef. See Net::HTTPS for documentation.
SSL_ca_file Sets the CA file name to use by the client (.crt). Includes full path and filename. Defaults to undef. See Net::HTTPS for documentation.
SSL_verify_mode Sets the verify mode to use for the connection. See Net::HTTPS for documentation.
KeepAlive Sets the keepalive-flag for the connection. Defaults to 1. See Net::HTTP for documentation.
useragent Sets the useragent string to appear as for the HTTPS-server. Default to "HTTPSClient/1.0".
converter Sets the Content-class type to use for the conversions to and from the HTTPS-server. Defaults to Content::JSON. See Content for documentation.
Besides this the constructor accepts any SSL parameter that the Net::HTTPS module accepts. See Net::HTTPS for more documentation.
Attempts to connect to the HTTPS-server. It first connects with an unencrypted HTTP-connection and then upgrades to an SSL-connection when successful.
Returns 1 if successful, 0 when failure. Check the error()-method for more details in such cases.
Checks to see if the client is connected to the HTTPS-server or not?
Returns 1 if it is, 0 if not.
Disconnects from the HTTPS-server if already connected.
Returns 1 if successful, 0 if disconnected already.
Executes a HTTP POST- og GET-method on the HTTPS-server and returns the result.
This method is not meant to be called directly, but can be if one so chooses to have the hassle with it.
The methods take these parameters in the following order:
servermethod The URL of the method to execute on the server. Eg. "/resource/getall". Defaults to "/status/alive".
httpmethod The HTTP method to use when executing the server-method. Defaults to "POST", but also accepts GET.
response A HASH-reference where the response from the server is placed after decoding.
parameters A HASH of parameters to the HTTPS-server. It is encoded into the Content-class type before being sent to the HTTPS-server. Defaults to undef.
This method takes the above parameters and then encodes the parameters based on the Content-class set for the instance and the executes a POST- or GET to the HTTPS-server. It then reads the response from the server and decodes it into HASH using the same Content-class instance. The resulting HASH is returned in the "response"-parameter above.
The method returns 1 upon successfully executing method on the HTTPS-server or 0 upon failure. Please check the error()-method for more information upon failure.
The response is read, as already mentioned, by looking at the HASH reference submitted to the method in the first place (parameter response).
A wrapper around the do()-method that is easier and quicker to use and that only performs HTTP POST-calls.
It takes these parameters in the following order:
servermethod The URL of the method to execute on the server. See the do()-method.
response A HASH-reference where the response from the server is placed after decoding. See the do()-method.
parameters A HASH of parameters to the HTTPS-server. See the do()-method.
This method executes the servermethod on the HTTPS-server by calling the do()-method.
The server response is returned in the already mentioned response-parameter.
The method returns 1 upon success, 0 upon failure. Please call the error()-method for more information on the failure.
Returns the last error message from the class.