现在的位置: 首页 >> 程序开发 >> C#.NET >> 使用C#与NNTP服务器交互!
添加时间:2005-10-30 来源:网教中国 作者:
使用C#与NNTP服务器交互!

using System;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Collections;
using System.Diagnostics;

namespace NntpTools {
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 class Debug {
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main(string[] args) {
   NNTPClass nc=new NNTPClass();
   nc.Connect("msnews.microsoft.com");
   ArrayList grouplist=nc.GetNewsgroups();
   for(int i=0;i<grouplist.Count;i++){
    Console.WriteLine(grouplist[i].ToString());
   }
   ArrayList cardlist=nc.GetNews("microsoft.public.cn.dotnet.framework");
   Console.WriteLine("=============================================================");
   StreamWriter sw=File.CreateText("c:\\news.txt");
   for(int i=0;i<cardlist.Count;i++){
    Console.WriteLine(cardlist[i].ToString());
    sw.WriteLine(cardlist[i].ToString());
    sw.WriteLine("=============================================================");
   }
   sw.Flush();
   sw.Close();
   nc.Disconnect();
  
   Console.ReadLine();
  }
 }

 class NNTPClass:System.Net.Sockets.TcpClient{
  public void Connect(string server){
   string response;

 

   Connect(server, 119);

   response = Response();

   if (response.Substring( 0, 3) != "200") {

    throw new Exception(response);

   }
  

  }

  public void Disconnect() {

   string message;

   string response;

 

   message = "QUIT\r\n";

   Write(message);

   response = Response();

   if (response.Substring( 0, 3) != "205") {

    throw new Exception(response);

   }

  }
  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 Exception(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;

    }

   }

  }
  public void Post(string newsgroup, string subject, string from,

   string content) {

   string message;

   string response;

 

   message = "POST " + newsgroup + "\r\n";

   Write(message);

   response = Response();

   if (response.Substring( 0, 3) != "340") {

    throw new Exception(response);

   }

 

   message = "From: " + from + "\r\n"

    + "Newsgroups: " + newsgroup + "\r\n"

    + "Subject: " + subject + "\r\n\r\n"

    + content + "\r\n.\r\n";

   Write(message);

   response = Response();

   if (response.Substring( 0, 3) != "240") {

    throw new Exception(response);

   }

  }


  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 Exception(response);

   }

 

   char[] seps = { ' ' };

   string[] values = response.Split(seps);



[1] [2]  下一页


上一篇:C#实现根据域名查询ip实例 下一篇:用C#生成中文汉字验证码的基本原理
大部分文章摘自网上,如有侵犯您的权益请与我们联系,我们会第一时间进行处理,谢谢! [ 打印文章 ] [ 关闭窗口 ]
推荐文章
·Snake.Net中的ORM(三)
·Visual C#托管Socket的实现方法(
·对.NET Framework "事件"机制理
·C#锐利体验(3.2)
·在C#中使用COM+实现事务控制
·.NET Remoting编程简介
·全面剖析VB.NET(3)
·微软的远程处理框架.NET Remotin
·C#重点知识详解(一)
·冰雹欲来风满楼--.NET计划初露锋
相关文章
 
最新文章
·数据结构与算法(C#实现)系列---(
·数据结构与算法(C#实现)系列---(
·数据结构与算法(C#实现)系列---(
·ASP.net 验证码(C#)
·Snake.Net中的ORM(三)
·Snake.Net中的ORM(二)
·Snake.Net中的ORM(-)
·用C#生成随机中文汉字验证码的基
·Autodesk官方最新的.NET教程(五
·Autodesk官方最新的.NET教程(四
Google