📄 nntpclient.cs
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
// Copyright (c) Telligent Systems Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Net.Sockets;
using System.Collections;
namespace AspNetForums.Components {
public class NntpClient {
private TcpClient connection = null;
private Stream stream = null;
private StreamReader reader = null;
private StreamWriter writer = null;
public NntpClient(String nntpHost) {
// Attempt to resolve the host name
//
if (Dns.GetHostByName(nntpHost) == null)
throw new Exception("Host: " + nntpHost + " does not exist or cannot be reached");
// Create a new tcp connection to the specified host
//
connection = new TcpClient(nntpHost, 119);
// Get a handle to the open TCP stream
//
this.stream = connection.GetStream();
// Get stream reader and writer
//
this.reader = new StreamReader(stream);
this.writer = new StreamWriter(stream);
String header = reader.ReadLine();
}
private NntpResponse SendCommand(NntpCommand command) {
return SendCommand(command, null);
}
private NntpResponse SendCommand(NntpCommand command, string commandParameters) {
#if DEBUG
Console.WriteLine("NNTP Send Command: " + command.ToString() + " " + commandParameters);
#endif
switch (command) {
case NntpCommand.Body:
WriteLine("body " + commandParameters);
break;
case NntpCommand.EndOfText:
WriteLine(".");
break;
case NntpCommand.Group:
WriteLine("group " + commandParameters);
break;
case NntpCommand.Head:
WriteLine("head " + commandParameters);
break;
case NntpCommand.List:
WriteLine("list " + commandParameters);
break;
case NntpCommand.Post:
WriteLine("post " + commandParameters);
break;
case NntpCommand.Xhdr:
WriteLine("xhdr " + commandParameters);
break;
}
String response = reader.ReadLine();
#if DEBUG
Console.WriteLine(response);
#endif
// Create the response class to return
//
ResponseCode responseCode = (ResponseCode) Int32.Parse(response.Substring(0, 3));
NntpResponse nntpResponse = new NntpResponse(responseCode, response);
return nntpResponse;
}
private void WriteLine(String line) {
writer.Write(line + "\r\n");
writer.Flush();
}
private NntpGroup ChangeGroup(string groupName) {
NntpGroup nntpGroup;
// Execute the NNTP change group command
//
NntpResponse nntpResponse = SendCommand(NntpCommand.Group, groupName);
// Ensure we got back the expected response
//
if (nntpResponse.ResponseCode != ResponseCode.GroupSelected) {
throw new Exception("Group: " + groupName + " does not exist on server");
}
// Parse out information about the group
//
nntpGroup = new NntpGroup(nntpResponse.RawResponse);
return nntpGroup;
}
public String [] GetGroups() {
ArrayList newsgroups = new ArrayList();
ResponseCode responseCode = SendCommand(NntpCommand.List).ResponseCode;
if (responseCode != ResponseCode.ListOfNewsGroupsFollows) {
throw new Exception("Error Obtaining Newsgroup List");
}
String responseLine = null;
while ((responseLine = reader.ReadLine()) != ".") {
newsgroups.Add( responseLine.Substring(0, responseLine.IndexOf(' ')) );
}
return (String []) newsgroups.ToArray(typeof(String));
}
public String [] GetArticleIDs(String group, int startPostNumber, int maxBackArticles) {
ArrayList articles = new ArrayList();
// Change to the requested group
//
NntpGroup nntpGroup = ChangeGroup(group);
// Ensure we're not getting back more articles then we want
//
if ( (nntpGroup.EndPostNumber - startPostNumber) > maxBackArticles )
startPostNumber = nntpGroup.EndPostNumber - maxBackArticles;
// Request a set of articles
//
ResponseCode responseCode = SendCommand(NntpCommand.Xhdr, "message-id " + startPostNumber + "-").ResponseCode;
// Handle error codes
//
if (responseCode == ResponseCode.NoSuchArticleFound) {
return new String[0];
} else if (responseCode != ResponseCode.ArticleRetrievedHeadFollows) {
throw new Exception("Error Obtaining Newsgroup Articles");
}
String responseLine = null;
#if DEBUG
Console.WriteLine("Reading articles...");
#endif
while ((responseLine = reader.ReadLine()) != ".")
articles.Add( responseLine );
return (String []) articles.ToArray(typeof(String));
}
// need to get the references header to
public Hashtable GetArticleHeaders(String articleID) {
Hashtable headers = new Hashtable();
ResponseCode responseCode = SendCommand(NntpCommand.Head, articleID).ResponseCode;
if (responseCode != ResponseCode.ArticleRetrievedHeadFollows) {
throw new Exception("Error Obtaining Article Header: " + articleID);
}
String responseLine = null;
while ((responseLine = reader.ReadLine()) != ".") {
int token = responseLine.IndexOf(":");
if (token != -1) {
headers[responseLine.Substring(0, token).ToLower()] = responseLine.Substring(token+1);
}
}
return headers;
}
public String GetArticleBody(String articleID) {
String body = "";
ResponseCode responseCode = SendCommand(NntpCommand.Body, articleID).ResponseCode;
if (responseCode != ResponseCode.ArticleRetrievedBodyFollows) {
throw new Exception("Error Obtaining Article Body: " + articleID);
}
String responseLine = null;
while ((responseLine = reader.ReadLine()) != ".") {
body += responseLine + "\r\n";
}
return body;
}
public NntpPost GetArticle(String articleID) {
return new NntpPost(articleID, GetArticleHeaders(articleID), GetArticleBody(articleID));
}
public void PostArticle(NntpPost post) {
ResponseCode responseCode = SendCommand(NntpCommand.Post).ResponseCode;
if (responseCode != ResponseCode.SendArticleToPost) {
throw new Exception("Error Posting Article!");
}
foreach (DictionaryEntry header in post.Headers) {
WriteLine(header.Key + ": " + header.Value);
}
WriteLine("");
WriteLine(post.Body);
responseCode = SendCommand(NntpCommand.EndOfText).ResponseCode;
if (responseCode != ResponseCode.ArticlePostedOk) {
throw new Exception("Error Completing the Posting of an Article!");
}
#if DEBUG
foreach(DictionaryEntry header in post.Headers) {
Console.WriteLine(header.Key + ":" + header.Value);
}
#endif
}
public void Close() {
reader.Close();
writer.Close();
stream.Close();
connection.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -