.Net Adventures

Appunti di viaggio dall'universo .Net
posts - 1, comments - 7, trackbacks - 2

giovedì 16 giugno 2005

Interrogare server Whois con .Net

Come spesso accade, ci si ritrova a risolvere, grazie al Framework, problemi apparentemente intricati con poche righe di codice.
E' il caso della classe che vi propongo, che esegue un'interrogazione ad un server Whois, ricevendo in cambio il record corrispondente (o un segnale di "not found") al dominio richiesto. Innanzitutto i namespace da importare, che sono: Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
La logica dietro a questa classe sarà quella di inzialmente impostare tramite delle property R/W il server Whois da interrogare, il dominio da verificare e l'eventuale porta TCP (di fatto è sempre la 43 per i Whois). Private m_WhoisServer As String = String.Empty
Private m_Port As Integer = 43
Private m_Domain As String = String.Empty

Public Property Domain() As String
Get
Return m_Domain
End Get
Set(ByVal Value As String)
m_Domain = Value + vbCrLf
End Set
End Property

Public Property Port() As Integer
Get
Return m_Port
End Get
Set(ByVal Value As Integer)
m_Port = Value
End Set
End Property

Public Property WhoisServer() As String
Get
Return m_WhoisServer
End Get
Set(ByVal Value As String)
m_WhoisServer = Value
End Set
End Property
Per comodità, un overload del costruttore accetta direttamente server e dominio da verificare: Public Sub New()
'
End Sub

Public Sub New(ByVal WhoisServer As String, ByVal Domain As String)
Me.WhoisServer = WhoisServer
Me.Domain = Domain
End Sub
Infine il codice principale, che esegue la query. Utilizzo un oggetto TcpClient, lo connetto al server e alla porta richiesto e imposto un timeout accettabile. Quindi passo, codificato in ASCII.GetBytes, il dominio da verificare. Da notare che quando viene impostata la property Domain, aggiungo alla fine il carattere di ritorno a capo: non settando questa chiusura, la richiesta al server rimane "appesa" (si può verificare la query anche tramite Telnet). Infine, leggo in uno streamreader il risultato restituito dal server. Public Function Query() As String

Dim TCPC As New TcpClient
Dim sReturn As String

Try
TCPC.Connect(Me.WhoisServer, Me.Port)
TCPC.ReceiveTimeout = 5000
Dim arrDomain As Byte() = Encoding.ASCII.GetBytes(Me.Domain.ToCharArray)
Dim s As Stream = TCPC.GetStream
s.Write(arrDomain, 0, Me.Domain.Length)
Dim sr As New StreamReader(TCPC.GetStream(), Encoding.ASCII)

sReturn = sr.ReadToEnd

TCPC.Close()
Catch e As Exception
sReturn = "Errore di comunicazione con il server " + Me.WhoisServer
End Try

Return sReturn

End Function
Ho utilizzato un blocco try-catch anche se preferisco sempre farne a meno, poichè ho sviluppato la classe in fretta e non ho potuto analizzare attentamente la gestione degli errori derivanti dall'oggetto TcpClient. Se qualcuno ha soluzioni più eleganti, sono sempre ben accette! That's all folks :) tK

posted @ giovedì 16 giugno 2005 10:32 | Feedback (9) | Filed Under [ .Net softcore ]

Powered by:
Powered By Subtext Powered By ASP.NET