ASP.Net WhoIs LookUp
Posted: 13/02/2005 14:32:08
Author: Andy Pilgrim
 ASP.Net 1.x Compatible (?)
Introduction
In Part 1 and Part 2 of this series, we examined how to use ASP.Net to perform
forward and reverse DNS Lookups. In this article we look at how we may use ASP.Net to query WhoIs servers.
What is whois?
Information about the owner of every domain on the internet is available to the public via an appropriate whois server. These records
typically contain the name, postal address and email address of the domain owner as well as the status and the expiry
date of the registration.
Different WhoIs servers are required for different tlds. In this article we restrict our attention to .com, .net and .edu domains, though
the concepts we discuss easily generalize to other tlds.
Communicating with the server
WhoIs servers listen on port 43 and accept input of a domain name (without the www). We first must allow ASP.Net to communicate with the
WhoIs server. This can easily be done using the System.Net.Sockets namespace, some time ago I wrote the following class to be included in a
library of network functions:
Public Class TCPAgent
'Basic TCP/IP communication agent
'TCP Connection
Dim TCPConn As New System.Net.Sockets.TCPClient
'Network Stream
Dim NetworkStream As System.Net.Sockets.NetworkStream
Sub Connect(Server As String, Port As Integer)
'Connects to a server, and gets the network stream
TCPConn.Connect(Server,Port)
NetworkStream = TCPConn.GetStream()
End Sub
Function Send(CommandString As String) As String
'Sends a command to the server and returns any response
'Turn string into an array of bytes
Dim CommandArray As Byte() = _
Encoding.ASCII.GetBytes(CommandString)
'Send the commnad
NetworkStream.Write(CommandArray, 0, CommandArray.Length)
'Return the response
Return Receive()
End Function
Function Receive() As String
'Returns any response from the server
'Declare array to receive the response
Dim Bytes(TCPConn.ReceiveBufferSize) As Byte
'Read the response into the array
Dim objSR as new System.io.StreamReader( _
tcpconn.GetStream(), Encoding.ASCII)
Receive = (objsr.readtoend())
End Function
Sub Close()
'Closes all connections
NetworkStream.Close()
TCPConn.Close()
End Sub
End Class
I won't spend too much time on this class, as the use of the TCP client classes is not the topic of the article, however, I think you’‘ll
find the code fairly self explanitory.
A WhoIs LookUp Function
Now we code a simple function to use the TCPAgent class defined above, again this is relatively simple:
Function WhoIsLookup(LookUpDomain As String, _
WhoIsServer As String) As String
Dim ServerConn As New TCPAgent
ServerConn.connect(WhoIsServer,43)
Return ServerConn.send(LookUpDomain & vbCrLf)
ServerConn.close()
End Function
The function creates a instance of the TCPAgent class connects to the supplied whois server on port 43, sends the whois server a command
to return the records for the given domain and returns them as a string. Note here the addition of vbCrLf, this is effectively the end
of command marker and tells the server to execute the command.
A Page to Call the Function
Using the following ASP.Net page we can invoke the function and return the result to the screen.
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net" %>
<html>
<head>
<title>ASP.Net WhoIs LookUp</title>
<style>
body {font-family:verdana}
</style>
</head>
<body>
<form runat="server">
<h1>WhoIs LookUp Tool</h1>
Domain: <asp:textbox runat="server" id="Domain" />
<asp:button onclick="doLookUp" runat="server" text="LookUp"/>
<h2>Results</h2>
<p><asp:label runat="server" id="whoisrecord" /></p>
</form>
</body>
</html>
<script runat="server">
Sub DoLookUp(Sender As Object, e As EventArgs)
WhoIsRecord.Text = _
WhoIsLookUp("domain " & Domain.Text,"rs.internic.net")
End Sub
...
</script>
This simple page accepts input of a domain name, calls the function to query the server and returns the result. Thats all there is to it! We
used the whois server rs.internic.net to perform the look up, though you could use anyone you like (as long as it contains records on the tld
you are lookin up). A list of whois servers and their corresponding tlds can be found at
http://www.math.utah.edu/whois.html.
Note here that we have prefixed the domain to look up
with the keyword "domain", this tells the whois server only to return an exact match for our domain and not variants on it. To see how this works
try looking up microsoft.com with and without the domain prefix code in place.
Improving the whois Tool
On Page 2 we look at how we can improve the results returned by the who is server, add handling for domains
which don’‘t have records and explore some regular expressions to format the results.
On to Page 2...
|