J
Jeremy Chapman
I've got a 2rd party API written in perl. We want to use it by converting
to a c# .net assembly. Essentially it does HTTP posts to a web page to
send/receive data. I've never done perl, and am hoping someone can help
with conversion, particularly around the setup of the $UA variable and doing
the post.
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Status;
use HTTP::Cookies;
use HTTP::Request::Common qw(POST);
use strict;
#Setup package Variables
my ($COOKIE_JAR, $UA, $WEBBASE, $DIR, $DEBUG);
my ($Messages, $Tid, $Result, $Filename) = ("", "", "", "");
#Setup package Constants
my ($RESPONSE, $TMPFILE) = ("response.tmp", "temp.tmp");
use vars qw ($NOT_IMPLEMENTED $ERROR $VALID $SUCCESS);
($NOT_IMPLEMENTED, $ERROR, $VALID, $SUCCESS) = (-2, -1, 0, 1);
1;
sub init
{
$WEBBASE = "https://localhost/test";
my ($timeout);
($WEBBASE, $timeout, $DIR, $DEBUG) = @_;
my ($cookiefile) = "cookies.txt";
if (-d $DIR)
{
$COOKIE_JAR = HTTP::Cookies->new(File => $DIR . $cookiefile,
AutoSave => 1,
ignore_discard => 1);
$UA = new LWP::UserAgent;
$UA->agent("TestPerl 1.0");
$UA->from("TestAPI");
$UA->timeout($timeout);
$UA->cookie_jar($COOKIE_JAR);
$COOKIE_JAR->save();
return $SUCCESS;
}
else
{
$Messages = "$DIR is not a valid directory";
return $ERROR;
}
}
sub login
{
my ($uid, $pw) = @_;
my $request = POST $WEBBASE, [ 'username' => $uid,
'password' => $pw,
'ExternalAction' => 'AsignOn'];
my $retVal = processRequest($request);
if ($retVal == $SUCCESS)
{#valid response
if ($Result ne "SUCCESS")
{
$retVal = $VALID;
}
}
else
{
$retVal = $ERROR;
}
return $retVal;
}
to a c# .net assembly. Essentially it does HTTP posts to a web page to
send/receive data. I've never done perl, and am hoping someone can help
with conversion, particularly around the setup of the $UA variable and doing
the post.
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Status;
use HTTP::Cookies;
use HTTP::Request::Common qw(POST);
use strict;
#Setup package Variables
my ($COOKIE_JAR, $UA, $WEBBASE, $DIR, $DEBUG);
my ($Messages, $Tid, $Result, $Filename) = ("", "", "", "");
#Setup package Constants
my ($RESPONSE, $TMPFILE) = ("response.tmp", "temp.tmp");
use vars qw ($NOT_IMPLEMENTED $ERROR $VALID $SUCCESS);
($NOT_IMPLEMENTED, $ERROR, $VALID, $SUCCESS) = (-2, -1, 0, 1);
1;
sub init
{
$WEBBASE = "https://localhost/test";
my ($timeout);
($WEBBASE, $timeout, $DIR, $DEBUG) = @_;
my ($cookiefile) = "cookies.txt";
if (-d $DIR)
{
$COOKIE_JAR = HTTP::Cookies->new(File => $DIR . $cookiefile,
AutoSave => 1,
ignore_discard => 1);
$UA = new LWP::UserAgent;
$UA->agent("TestPerl 1.0");
$UA->from("TestAPI");
$UA->timeout($timeout);
$UA->cookie_jar($COOKIE_JAR);
$COOKIE_JAR->save();
return $SUCCESS;
}
else
{
$Messages = "$DIR is not a valid directory";
return $ERROR;
}
}
sub login
{
my ($uid, $pw) = @_;
my $request = POST $WEBBASE, [ 'username' => $uid,
'password' => $pw,
'ExternalAction' => 'AsignOn'];
my $retVal = processRequest($request);
if ($retVal == $SUCCESS)
{#valid response
if ($Result ne "SUCCESS")
{
$retVal = $VALID;
}
}
else
{
$retVal = $ERROR;
}
return $retVal;
}