Hi Rob,

I am sorry, I somehow missed your response until this evening and in the mean 
time have figured out lots of new ways to have this fail <shrug>.

Anyway, I looked at your sample and came up with the following updates and it 
works but I am interested to hear your input on how I can improve it.

#!/opt/csw/bin/perl
use strict;
use warnings;

use LWP;
use XML::Smart;
use CGI;

my $ua = LWP::UserAgent->new;
my $q = new CGI;

my $doc = XML::Smart->new();
$doc->{PHC_LOGIN}{USERID} = ‘john.doe';
$doc->{PHC_LOGIN}{USERID}->set_node(1);
$doc->{PHC_LOGIN}{USERPASSWORD} = ‘FAKEPASSWORD;
$doc->{PHC_LOGIN}{USERPASSWORD}->set_node(1);
$doc->{PHC_LOGIN}{PARTNERID} = ‘111';
$doc->{PHC_LOGIN}{PARTNERID}->set_node(1);

$doc->save('passport.xml');

my $sendXML = "passport.xml";

my $message="";
open (XML,$sendXML);
while (<XML>) {
        $message .="$_";
}
close XML;

my $webpage ="https://example.com/members/LoginNet/AutoLogin.aspx";;

$ua->proxy('https', 'http://proxy.exampe.net:8080/');
my $resp = $ua->post($webpage, Content_Type => 'text/xml', Content => $message);

my $xml = XML::Smart->new($resp->decoded_content);
my $dest = $xml->{PHC_LOGIN}{REDIRECTURL};
print "Location: $dest\n\n";


On Aug 22, 2014, at 10:38 AM, Rob Dixon <rob.di...@gmx.com> wrote:

> On 22/08/2014 11:27, angus wrote:
>> Hi,
>> 
>> Hi,
>> 
>> I have some sample code that posts some XML content to an URL using 
>> LWP agent and XML Smart.  The response to this comes back as some 
>> custom XML.  I now need to try and parse the response and find the 
>> REDIRECTURL value and redirect the client to that destination.
>> 
>> I am having issues parsing the output here is what I have tried.
>> 
>> my $response = $ua->post($webpage, Content_Type => 'text/xml', 
>> Content => $message); $response->decoded_content;
>> 
>> if ($response->is_success) { my $xml = new XML::Smart( 
>> "$resonse->content" , 'XML::Smart::HTMLParser' ); my $xml = new 
>> XML::Smart( "$resonse->content" , 'html' ); print "\n\n 
>> FOUND:$xml\n"; # comes back with a null value
>> 
>> ########################################################## # another 
>> attempt below ###############################
>> 
>> my $response = $ua->post($webpage, Content_Type => 'text/xml', 
>> Content => $message); $response->decoded_content;
>> 
>> if ($response->is_success) { my $html = $response->content; while ( 
>> $html =~/REDIRECT/g ) { print "I found: $1\n"; } else { print 
>> “nothing found\n”; };
>> 
>> The find the value REDIRECT as the script prints out “I found: “ and 
>> not “nothing found” but I need the url value….
>> 
>> Here is a sample of what the xml looks like that I am trying to 
>> parse.
>> 
>> <PHC_LOGIN>\r <VERSION>3.0</VERSION>\r 
>> <PARTNERID>111111</PARTNERID>\r <USERLOGIN>John.doe</USERLOGIN>\r 
>> <ERROR_CODE>0</ERROR_CODE>\r <ERROR_DESCRIPTION>Login 
>> Successful</ERROR_DESCRIPTION>\r 
>> <REDIRECTURL>https://example.com/cust/login.asp</REDIRECTURL>\r 
>> </PHC_LOGIN>
> 
> There are a few problems here. I am concentrating on your first attempt,
> as regular expressions are very rarely an appropriate way of processing XML.
> 
> - You need to save the content of the incoming message so that you can
> pass it to the parser, but you have written
> 
>   $response->decoded_content
> 
> in void context, so the result will be just thrown away
> 
> - In the line
> 
>   my $xml = new XML::Smart( "$resonse->content" ,
> 'XML::Smart::HTMLParser' );
> 
> you mustn't put the method call in quotation marks.It will try to
> stringify `$response` (which, incidentally, you have misspelled) and
> result in something like
> 
>   HTTP::Response=HASH(0x2c68544)->content
> 
> Plus, to be safe, you should be calling `decoded_content` instead of
> just `content`.
> 
> - There is no need to specify a parser in the second parameter unless
> you need specific behaviour, so that line should be
> 
>   my $xml = XML::Smart->new( $resp->decoded_content )
> 
> after which you can simply access the elemetn you need using
> 
>   $xml->{PHC_LOGIN}{REDIRECTURL}
> 
> I have put a semi-complete program below. It needs values for $webpage
> and $message, but the rest of the code is in place, and it runs fine.
> 
> HTH,
> 
> Rob
> 
> 
> 
>   use strict;
>   use warnings;
>   use 5.010;
> 
>   use LWP;
>   use XML::Smart;
> 
>   my $ua = LWP::UserAgent->new;
> 
>   my $webpage = '';
>   my $message = '';
> 
>   my $resp = $ua->post($webpage, Content_Type => 'text/xml', Content =>
> $message);
> 
>   my $xml = XML::Smart->new( $resp->decoded_content );
>   say $xml->{PHC_LOGIN}{REDIRECTURL};
> 
> **output**
> 
>   https://example.com/cust/login.asp
> 

Reply via email to