Using Perl LWP::UserAgent with XML that requires Usernames and Passwords to connect

1.2k views Asked by At

OK... I'm trying to post an XML page to a strategic partner's site for them to do some calculations and return me a response code, again via XML.

I'm using garden-variety PERL, and was trying to use LWP::UserAgent to do this... if there is a more elegant way, please point me in that direction.

I'm able to hit their server, and get a code back... but it denies access to their server because the username and password it is expecting is not correct. The username and password are not specified in the actual XML code, it is supposed to be passed to the server as part of the actual POST method... but I have no clue how to do that.

The partner gives the following feedback:

replacing the YOUR_XML_FILE, YOUR_USER and YOUR_PASSWORD with his xml that gets generated, and his username/password combination. This should be done from the server that he has his perl script.

wget --no-check-certificate --post-file YOUR_XML_FILE https://previewtest.clverify.com/webservice/exec -O previewsamplerequest.response.xml --http-user=YOUR_USER --http-password=YOUR_PASSWORD

I don't know where this is supposed to be generated, or what the options are in LWP::UserAgent to specify them. Has anyone out there done this before?

Here is my code:

sub ConsumerInfo {
my $cid = shift;

my $response = undef;
my $sendXML = &Create_ConsumerInfo_Request($cid);
if ($sendXML) {
    &DoXMLUpload($sendXML);

    my $browser = LWP::UserAgent->new(agent => 'perl post');
    $browser->credentials('','','username','p@ssword');
    $response = $browser->request(POST 'https://previewtest.clverify.com/webservice/exec',
        Content_Type => 'text/xml',
        Content => $sendXML);
    print "Content-type:text/html\n\n";
    print $response->error_as_HTML unless $response->is_success;
    print $response->as_string;

} else {
    &ErrorMsg("No XML Code Was Found.");
    exit;   
}
# ===============================================================
# Need to insert parser in here to convert this into an array.
# ===============================================================
return $response;
}
1

There are 1 answers

3
Erik On

WARNING: You provided the real URL and password. Change this ASAP, since everybody can connect to the server of your partner now!!

Imho this can be found in the documentation:

$ua->credentials( $netloc, $realm, $uname, $pass )
Get/set the user name and password to be used for a realm. The $netloc is a string of the form ":". The username and password will only be passed to this server.

The example shows the correct way to authenticate:

$ua->credentials("www.example.com:80", "Some Realm", "foo", "secret");

In your case this would mean:

$browser->credentials('previewtest.clverify.com:443','yourRealm','321321','Eep789SHag@');