/*
* C# TravelFusion Example
*
* User: craig
* Date: 06/03/2007
* Time: 15:30
*
* This Application will perform a simple 'Login' Command,
* it will detect failures such as invalid Username / Password
* and return them to the person using it. Otherwise it will display
* the XML received from the Server for later Parsing.
*
*/
using System;
using System.IO;
using System.Xml;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.Collections;
namespace TF
{
class MainClass
{
int port = 80;
String host = "(URL address blocked: See forum rules)";
String username = "UserName";
String password = "Password";
public static void Main(string[] args)
{
MainClass main = new MainClass();
main.run();
Console.ReadKey();
}
private void generateXml(XmlTextWriter writer) {
writer.Formatting = Formatting.Indented;
writer.WriteStartElement("CommandList");
writer.WriteStartElement("Login", null);
writer.WriteElementString("Username", this.username);
writer.WriteElementString("Password", this.password);
writer.WriteEndElement();
writer.WriteEndElement();
}
public void run() {
// Create a MemoryStream for the XmlTextWriter to dump into
// (We need .Length to send as a Content-Length)
MemoryStream s = new MemoryStream();
// Prepare the XmlTextWriter with our MemoryStream
XmlTextWriter writer = new XmlTextWriter(s, Encoding.ASCII);
// Call our Xml Generator..
this.generateXml(writer);
// Flush the Xml Generated into our stream.
writer.Flush();
// Create the Socket..
TcpClient socket = new TcpClient(this.host, this.port);
// Grab the network stream, and send the HTTP Headers..
NetworkStream httpStream = socket.GetStream();
sendString(httpStream, "POST /Xml HTTP/1.0\r\n");
sendString(httpStream, "Content-Type: text/xml\r\n");
// This is a pre-requisite for the way we're sending Xml..
sendString(httpStream, "Content-Length: " + s.Length + "\r\n");
sendString(httpStream, "\r\n");
// Pull our Xml out the MemoryStream, and send it.
sendString(httpStream, System.Text.Encoding.ASCII.GetString(s.GetBuffer() ));
// Send the following to let The web server know we're finished.
sendString(httpStream, "\r\n\r\n");
// Our MemoryStream is no longer required, close it here.
writer.Close();
// Prepare a Read buffer, as well as StringBuilder to receive the response..
byte[] readBuffer = new Byte[2048];
StringBuilder complete = new StringBuilder();
int numBytes = 0;
if (httpStream.CanRead) {
do {
// Pull the Response into the Builder..
numBytes = httpStream.Read(readBuffer, 0, readBuffer.Length);
complete.AppendFormat("{0}", Encoding.UTF8.GetString(readBuffer, 0, numBytes));
} while (httpStream.DataAvailable);
} else {
// Premature Closing of Socket?
return;
}
// Strip Headers and Load the response into a String..
String Xml = complete.ToString().Substring(complete.ToString(). IndexOf("\r\n\r\n") + 4);
// Create a new MemoryStream to Store the Response Xml so it can be read using XmlTextReader
MemoryStream r = new MemoryStream(ASCIIEncoding.Default.GetBytes(Xml));
// Perform the reading..
XmlTextReader reader = new XmlTextReader(r);
while (reader.Read()) {
switch (reader.NodeType)
{
case XmlNodeType.Element:
// We have a tag, Verify it's what we wanted!
if (reader.Name == "CommandExecutionFailure") {
// Execution of the Command Failed, Find the original Tag..
reader.ReadToFollowing("Login");
// Then the error text attribute..
while (reader.MoveToNextAttribute()) {
if (reader.Name == "etext") {
Console.WriteLine("Login Failed: " + reader.Value);
}
}
// Bail out here.
Console.WriteLine("Press any key to continue..");
Console.ReadKey();
return;
}
break;
// case ...:
}
}
// If we get to here, an error hasn't been found.
Console.WriteLine(Xml);
Console.WriteLine("Press any key to continue..");
Console.ReadKey();
}
private void sendString(NetworkStream httpStream, String text) {
byte[] data = new Byte[2084];
data = System.Text.Encoding.UTF8.GetBytes(text);
httpStream.Write(data, 0, data.Length);
}
}
}