Library tutorials & articles

How to NNTP in C#

Getting the Newsgroups

The GetNewsgroups method, receives from the NNTP server all the forums that are supported by the server.

public ArrayList GetNewsgroups()
{
    string message;
    string response;

    ArrayList retval = new ArrayList();
    message = "LIST\r\n";
    Write(message);
    response = Response();
    if (response.Substring(0, 3) != "215")
    {
        throw new NntpException(response);
    }

    while (true)
    {
        response = Response();
        if (response == ".\r\n" ||
            response == ".\n")
        {
            return retval;
        }
        else
        {
            char[] seps = {' ' };
            string[] values = response.Split(seps);
            retval.Add(values[0]);
            continue;
        }
    }
}

The GetNewsgroups method begins by sending a LIST message to the NNTP server. The NNTP server will respond initially with the 215 status-code indicating that it successfully received the LIST message. Then the NNTP server will respond with a series of lines, each representing one forum on the NNTP server. After all the forums are sent, the NNTP server will send one line with a single period, indicating the end of the forum list. The list of forums is returned from the GetNewsgroups method as an ArrayList of strings.

From the list of forums, you can select one forum and receive from the GetNews method all the news for that forum. Call GetNews passing the name of the forum to receive the news postings.

public ArrayList GetNews(string newsgroup)
{
    string message;
    string response;

    ArrayList retval = new ArrayList();
    message = "GROUP " + newsgroup + "\r\n";
    Write(message);
    response = Response();
    if (response.Substring(0, 3) != "211")
    {
        throw new NntpException(response);
    }

    char[] seps = {' ' };
    string[] values = response.Split(seps);

    long start = Int32.Parse(values[2]);
    long end = Int32.Parse(values[3]);

    if (start+ 100 < end && end > 100)
    {
        start = end-100;
    }

    for (long i= start; i< end; i++)
    {
        message = "ARTICLE " + i + "\r\n";
        Write(message);
        response = Response();
        if (response.Substring(0, 3) == "423")
        {
            continue;
        }
        if (response.Substring(0, 3) != "220")
        {
            throw new NntpException(response);
        }

        string article = "";
        while (true)
        {
            response = Response();
            if (response == ".\r\n")
            {
                break;
            }

            if (response == ".\n")
            {
                break;
            }
            if (article.Length < 1024)
            {
                article += response;
            };
        }

        retval.Add(article);
    }

    return retval;
}

The GetNews method sends a GROUP message to the NNTP server. The NNTP server will responds with a status-code of 211, indicating success, the numbers of articles in the forum present the server, the lowest message number for an article in the forum and the highest message number for an article in the forum. The method then repeatedly sends an ARTICLE message requesting each article between the lowest and highest message numbers. The NNTP server responds with a 423 status-code if the article is not present on the server and a 220 status-code if the article is present. When we receive a 423 status-code, then we skip to the next message number. When we receive a 220 status-code, the status line is followed by the content of the message and terminated with the now familiar with line with only one period. As the articles are received then are placed into an ArrayList object and returned from the GetNews function once all messages are received.

Comments

  1. 08 Apr 2006 at 22:54

    Great article - thanks. 

    However I'd like to post HTML to an NNTP server.  I'm assuming that I need to specify this in the Header info somehow but can't find any documentation on NNTP message headers.

    Could you shed some light here.

    Thanks again

     

  2. 20 Apr 2005 at 21:35

    The only problem I found with this article, was the missing "Supporting Functions" you included in your other programs.  Other than that, this is an awesome article!  Keep up the good work!

  3. 01 Oct 2004 at 08:59


    This saved me days of work ! Thanks, really excellent.

  4. 01 Jan 1999 at 00:00

    This thread is for discussions of How to NNTP in C#.

Leave a comment

Sign in or Join us (it's free).

Randy Charles Morin Randy's article are Copyright 1998-2003 Randy Charles Morin

Related podcasts

  • Object-Oriented Programming in Ruby

    In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...

Want to stay in touch with what's going on? Follow us on twitter!