Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->编程语言 ->ASP.NET ->正文

使用C#与NNTP服务器交互!

来源:Linuxdby.com 作者:Webmaster 时间:2007-06-04 点击: [收藏] [投稿]

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);

 

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



 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:用C#生成中文汉字验证码的基本原理   下一篇:C#实现根据域名查询ip实例

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章
Power by linux-cn.com 粤ICP备05006655号