示例#1
0
        /// <summary>
        /// Posts specified message to the specified newsgroup.
        /// </summary>
        /// <param name="newsgroup">Newsgroup where to post message.</param>
        /// <param name="message">Message to post. Message is taken from stream current position.</param>
        /// <exception cref="ObjectDisposedException">Is raised when this object is disposed and this method is accessed.</exception>
        /// <exception cref="InvalidOperationException">Is raised when NNTP client is not connected.</exception>
        public void PostMessage(string newsgroup, Stream message)
        {
            /* RFC 977 3.10.1.  POST
             *
             *  If posting is allowed, response code 340 is returned to indicate that
             *  the article to be posted should be sent. Response code 440 indicates
             *  that posting is prohibited for some installation-dependent reason.
             *
             *  If posting is permitted, the article should be presented in the
             *  format specified by RFC850, and should include all required header
             *  lines. After the article's header and body have been completely sent
             *  by the client to the server, a further response code will be returned
             *  to indicate success or failure of the posting attempt.
             *
             *  The text forming the header and body of the message to be posted
             *  should be sent by the client using the conventions for text received
             *  from the news server:  A single period (".") on a line indicates the
             *  end of the text, with lines starting with a period in the original
             *  text having that period doubled during transmission.
             *
             *  No attempt shall be made by the server to filter characters, fold or
             *  limit lines, or otherwise process incoming text.  It is our intent
             *  that the server just pass the incoming message to be posted to the
             *  server installation's news posting software, which is separate from
             *  this specification.  See RFC850 for more details.
             *
             *  Example:
             *      C: POST
             *      S: 340 Continue posting; Period on a line by itself to end
             *      C: (transmits news article in RFC850 format)
             *      C: .
             *      S: 240 Article posted successfully.
             */

            if (IsDisposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }
            if (!IsConnected)
            {
                throw new InvalidOperationException("NNTP client is not connected.");
            }

            // Send POST command
            WriteLine("POST");

            // Read server response
            string responseLine = ReadLine();

            if (!responseLine.StartsWith("340"))
            {
                throw new Exception(responseLine);
            }

            // POST message
            TcpStream.WritePeriodTerminated(message);

            // Read server response
            responseLine = ReadLine();
            if (!responseLine.StartsWith("240"))
            {
                throw new Exception(responseLine);
            }
        }