Matrix Science header

http_helper_getstring.pl

Accessing any server using http(s)

#!/usr/local/bin/perl
##############################################################################
# File: http_helper_getstring.pl                                             #
# Mascot Parser toolkit example code                                         #
##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 1998-2015 Matrix Science Limited  All Rights Reserved.           #
#                                                                            #
##############################################################################
#     $Source: parser/examples/test_perl/http_helper_getstring.pl $
#     $Author: villek@matrixscience.com $
#       $Date: 2018-07-30 16:23:53 +0100 $
#   $Revision: 1b450440f9c97e1e41d0fc6016a27d68951d4532 | MSPARSER_REL_2_8_1-0-gea32989045 $ 
##############################################################################
use strict;
use warnings;
##############################################################################

# required Perl packages
use lib "../bin";
use msparser;
use Getopt::Long;
use File::Basename;

my ($url, $action, $httpUserName, $httpPassword, $username, $password);
$username = "";
$password = "";

GetOptions('url=s'          => \$url,
           'action=s'       => \$action,
           'httpUserName=s' => \$httpUserName,
           'httpPassword=s' => \$httpPassword,
           'username=s'     => \$username,
           'password=s'     => \$password); 

if (not defined $url or not defined $action) {
   print <<USAGE;

Usage http_client.pl --url=<URL> --action=<command> [options]

    --url           Base URL, e.g 
                      http://your-server/
    --action        An action for a get command. 
                      Appended to the base URL
    --httpUserName  the username for an authenticated web server. 
    --httpPassword  the password for an authenticated web server. 
    --username      Mascot Server user name
                      May be required if Mascot Security is enabled
    --password      Mascot Server password
                      May be required if Mascot Security is enabled

USAGE
  exit 1;
}

# Common code for all cases

# Create connection settings. 
# Any settings for web server authentication or a proxy server should be set here
my $objSettings = new msparser::ms_connection_settings;
$objSettings->setUserAgent("PerlScriptTest/1.0 " . $objSettings->getUserAgent);

if ($httpUserName) {
  $objSettings->setHttpUsername($httpUserName);
}

if ($httpPassword) {
  $objSettings->setHttpPassword($httpPassword);
}

# Connect to the server
my $httpHelper = new msparser::ms_http_helper($url, $objSettings);

if (!$httpHelper->isValid()) {
  showErrorsAndExit($httpHelper);
}

my ($errCodeString, $retString) = $httpHelper->httpGetString($action);

if ($errCodeString->isOk()) {
  print "httpGetString OK : " . $retString . "\n";
} else {
  print "Error: " . $errCodeString->getErrorText() . "\n";
}

my ($errCodeHeader, $retHeader) = $httpHelper->httpGetHeader($action);

if ($errCodeHeader->isOk()) {
  print "httpGetHeader OK : " . $retHeader . "\n";
} else {
  print "Error: " . $errCodeHeader->getErrorText() . "\n";
}

my ($errCodeBufferOpen) = $httpHelper->httpBufferedOpen($action);

if ($errCodeBufferOpen->isOk()) {
  print "httpBufferedOpen OK : \n";
    while (1)
    {
        my ($errCodeBufferString, $retBufferString) = $httpHelper->httpBufferedGetString(10000);
        if (! length $retBufferString || !$errCodeBufferString->isOk())
        {
            last;
        }
        print $retBufferString;
    }
    $httpHelper->httpBufferedClose();
} else {
  print "Error: " . $errCodeBufferOpen->getErrorText() . "\n";
}

sub showErrorsAndExit {
    my ($obj) = @_;
    print "Error: ", $obj->getLastErrorString(), "\n";

    my $err = $obj->getErrorHandler();
    my $i;

    for my $i (1 .. $err->getNumberOfErrors) {
        print "Error number: ";
        print $err->getErrorNumber($i);
        print " (";
        print $err->getErrorRepeats($i) + 1;
        print " times) : ";
        print $err->getErrorString($i), "\n";
    }

    print "\n";

    exit -1;
}


Copyright © 2022 Matrix Science Ltd.  All Rights Reserved. Generated on Thu Mar 31 2022 01:12:29