Any perl folks can help with some translation?

  • Thread starter Thread starter Jeremy Chapman
  • Start date Start date
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;
}
 
Jeremy,

I don't know PERL, but it looks to me like you would want to use the
HttpWebRequest and HttpWebResponse classes in order to perform this
request/response. It shouldn't be too hard to configure the properties from
the values in this script.

Hope this helps.
 
I've found the HttpWebRequest and response classes, but I'm not sure what
the POST $WEBBASE, [ 'username' => $uid,'password' =>
$pw,'ExternalAction' => 'AsignOn']; statement does to the request. Also I
don't know what the $UA->from("TestAPI"); line sets either.

Nicholas Paldino said:
Jeremy,

I don't know PERL, but it looks to me like you would want to use the
HttpWebRequest and HttpWebResponse classes in order to perform this
request/response. It shouldn't be too hard to configure the properties
from the values in this script.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jeremy Chapman said:
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;
}
 
Jeremy,

If I had to guess, username and password would be set on the Credentials
property of the HttpWebRequest.

The $UA->from("TestAPI") call sets the From header, which you can set
through the Headers collection.

I was able to figure that out from the following document on Goggle:

http://kobesearch.cpan.org/htdocs/libwww-perl/LWP/UserAgent.html

I basically searched for "LWP::UserAgent".

As for the external action, I'm not so sure, but you should be able to
find some documentation for the libraries that script imports and figure out
what it does.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Jeremy Chapman said:
I've found the HttpWebRequest and response classes, but I'm not sure what
the POST $WEBBASE, [ 'username' => $uid,'password' =>
$pw,'ExternalAction' => 'AsignOn']; statement does to the request. Also I
don't know what the $UA->from("TestAPI"); line sets either.

Nicholas Paldino said:
Jeremy,

I don't know PERL, but it looks to me like you would want to use the
HttpWebRequest and HttpWebResponse classes in order to perform this
request/response. It shouldn't be too hard to configure the properties
from the values in this script.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jeremy Chapman said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top