Пример #1
0
        /// <summary>
        /// Parses from string.
        /// </summary>
        /// <param name="response">The response.</param>
        /// <returns></returns>
        public static CtchResponse ParseFromString(string response)
        {
            CtchResponse ctchResponse = new CtchResponse()
            {
                FullResponse = response
            };

            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StringReader sr = new StringReader(response))
                {
                    string line;
                    // Read and display lines from the file until the end of
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null)
                    {
                        string headerName = ReadHeaderName(line);

                        switch (headerName.ToUpper())
                        {
                        case "X-CTCH-PVER":
                            ctchResponse.Version = ReadHeaderValue(line); break;

                        case "X-CTCH-SPAM":
                            ctchResponse.SpamClassification = (CtchSpam)Enum.Parse(typeof(CtchSpam), ReadHeaderValue(line), true); break;

                        case "X-CTCH-VOD":
                            ctchResponse.VodClassification = (CtchVod)Enum.Parse(typeof(CtchVod), ReadHeaderValue(line), true); break;

                        case "X-CTCH-FLAGS":
                            ctchResponse.CtchFlag = ReadHeaderValue(line); break;

                        case "X-CTCH-REFID":
                            ctchResponse.RefID = ReadHeaderValue(line); break;

                        default:
                            ctchResponse.Headers.Add(headerName, ReadHeaderValue(line)); break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The response could not be read:");
                Console.WriteLine(e.Message);
            }

            return(ctchResponse);
        }
Пример #2
0
        /// <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 {2}",
                                           (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);
        }
Пример #3
0
        /// <summary>
        /// Parses from string.
        /// </summary>
        /// <param name="response">The response.</param>
        /// <returns></returns>
        public static CtchResponse ParseFromString(string response)
        {
            CtchResponse ctchResponse = new CtchResponse();

            ctchResponse.FullResponse = response;

            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StringReader sr = new StringReader(response))
                {
                    String line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null)
                    {
                        string headerName = ReadHeaderName(line);

                        switch (headerName.ToUpper())
                        {
                            case "X-CTCH-PVER":
                                ctchResponse.Version = ReadHeaderValue(line); break;
                            case "X-CTCH-SPAM":
                                ctchResponse.SpamClassification = (CtchSpam)Enum.Parse(typeof(CtchSpam), ReadHeaderValue(line), true); break;
                            case "X-CTCH-VOD":
                                ctchResponse.VodClassification = (CtchVod)Enum.Parse(typeof(CtchVod), ReadHeaderValue(line), true); break;
                            case "X-CTCH-FLAGS":
                                ctchResponse.CtchFlag = ReadHeaderValue(line); break;
                            case "X-CTCH-REFID":
                                ctchResponse.RefID = ReadHeaderValue(line); break;
                            default:
                                ctchResponse.Headers.Add(headerName, ReadHeaderValue(line)); break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The response could not be read:");
                Console.WriteLine(e.Message);
            }

            return ctchResponse;
        }