/// <summary>
/// Queries the server.
/// </summary>
/// <param name="host">The host.</param>
/// <param name="port">The port.</param>
/// <param name="message">The message.</param>
/// <param name="filename">The filename.</param>
/// <returns></returns>
private static CtchResponse QueryServer(string host, int port, Message message, string filename)
{
bool reference = true;
if (message != null)
{
reference = false;
}
else
{
message = Parser.ParseMessageFromFile(filename);
}
string version = "0000001";
// Prepare the commtouch headers
string content = string.Format("X-CTCH-PVer: {0}\r\nX-CTCH-MailFrom: {1}\r\nX-CTCH-SenderIP: {2}\r\n", version, message.Sender.Email, message.SenderIP);
if (reference)
{
content += string.Format("X-CTCH-FileName: {0}\r\n", filename);
}
else
{
content += string.Format("\r\n{0}", message.ToMimeString());
}
// Prepare the request with HTTP header
string request = string.Format("POST /ctasd/{0} HTTP/1.0\r\nContent-Length: {1}\r\n\r\n", (reference ? "ClassifyMessage_File" : "ClassifyMessage_Inline"), content.Length)
+ content;
//try
//{
TcpClient client = new TcpClient();
client.Connect(host, port);
Byte[] data = System.Text.Encoding.ASCII.GetBytes(request);
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
#if DEBUG
Console.WriteLine("<requestSent>");
Console.WriteLine("{0}", request);
Console.WriteLine("</requestSent>");
#endif
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
#if DEBUG
Console.WriteLine("<responseReceived>");
Console.WriteLine("{0}", responseData);
Console.WriteLine("</responseReceived>");
#endif
CtchResponse ctchResponse = CtchResponse.ParseFromString(responseData);
#if DEBUG
Console.WriteLine(ctchResponse.ToString());
#endif
// Close everything.
stream.Close();
client.Close();
//}
//catch (ArgumentNullException e)
//{
// Console.WriteLine("ArgumentNullException: {0}", e);
//}
//catch (SocketException e)
//{
// Console.WriteLine("SocketException: {0}", e);
//}
return(ctchResponse);
}