I am trying to recreate some code written in C# using perl. The goal is to
create an XML formatted login packet and post it to http webserver and then
parse the response. My first hurdle I think is to create the XML login packet.
Here is the sample of what the login packet is supposed to look like
<header>
</header>
<data>
<struct>
<var name='USERID'><string>john.doe</string></var>
<var name='USERPASSWORD'><string>FAKEPASSWD</string></var>
<var name='PARTNERID'><string>1</string></var>
<var name='HOMEURL'><string></string></var>
<var name='ERRORURL'><string></string></var>
</struct>
</data>
I have been looking for a perl module to help me create this and I think using
XML::DOM may solve this need.
I have the following code so far which almost provides the first attribute and
string value.
#!/usr/bin/perl
use strict;
use warnings;
use XML::DOM;
my $doc = XML::DOM::Document->new;
my $xml_pi = $doc->createXMLDecl ('1.0');
my $root = $doc->createElement('data');
my $body = $doc->createElement('struct');
$root->appendChild($body);
my $var = $doc->createElement('var');
$var->setAttribute('name', 'USERID');
$body->appendChild($var);
my $text = $doc->createTextNode('ghct.test');
$body->appendChild($text);
print $xml_pi->toString;
print $root->toString;
print "\n";
When I run this code I get back the following output.
<?xml version="1.0"?><data><struct><var name="USERID"/>john.doe</struct></data>
Note I am missing the <string> element.
Here is C# code used for this purpose.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Xml;
namespace LoginSSOSample
{
class Program
{
static void Main(string[] args)
{
string url = "https://example.com/external/login/autologin_xml.asp";
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(url);
ASCIIEncoding encoding = new ASCIIEncoding();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<TST_LOGIN></TST_LOGIN>");
var root = xmlDoc.DocumentElement;
root.SetAttribute("USERID", "john.doe");
root.SetAttribute("USERPASSWORD", "FAKEPASSWD");
root.SetAttribute("USERAUTH", "Client");
root.SetAttribute("PARTNERID", "1");
root.SetAttribute("ERRORURL", "");
root.SetAttribute("HOMEURL", "");
string postData = "TST_LOGIN=" +
Uri.EscapeUriString(xmlDoc.OuterXml);
byte[] data = encoding.GetBytes(postData);
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (Stream stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
string responseString = new
StreamReader(response.GetResponseStream()).ReadToEnd();
Debug.WriteLine(responseString);
xmlDoc.LoadXml(responseString);
root = xmlDoc.DocumentElement;
string rurl = root.GetAttribute("REDIRECTURL");
Process.Start(rurl);
}
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0,
bytes.Length);
return bytes;
}
}
}
thanks in advance for any assistance you may be able to provide.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/