We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 35,235 times

Contents

Related Categories

How to NNTP in C# - Getting the Newsgroups

randy

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.

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

Comments

  • Re: [4472] How to NNTP in C#

    Posted by RonaldJJames on 08 Apr 2006

    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...

  • The Only Problem...

    Posted by iancaz on 20 Apr 2005

    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!

  • Excellent tutorial

    Posted by alberto1964 on 01 Oct 2004


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