By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,724 Members | 859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,724 IT Pros & Developers. It's quick & easy.

Any perl folks can help with some translation?

P: n/a
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;
}
Aug 2 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Jeremy Chapman" <please@Idontlikespamwrote in message
news:O0*************@TK2MSFTNGP03.phx.gbl...
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;
}

Aug 2 '06 #2

P: n/a
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:uL**************@TK2MSFTNGP04.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Jeremy Chapman" <please@Idontlikespamwrote in message
news:O0*************@TK2MSFTNGP03.phx.gbl...
>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;
}


Aug 2 '06 #3

P: n/a
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/li...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]
- mv*@spam.guard.caspershouse.com
"Jeremy Chapman" <please@Idontlikespamwrote in message
news:uJ**************@TK2MSFTNGP04.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:uL**************@TK2MSFTNGP04.phx.gbl...
>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]
- mv*@spam.guard.caspershouse.com

"Jeremy Chapman" <please@Idontlikespamwrote in message
news:O0*************@TK2MSFTNGP03.phx.gbl...
>>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;
}



Aug 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.