ARTICLE

WhoIs in ASP.NET and VB.NET

Posted by Joydeep Ghosh Articles | Visual Basic 2010 August 02, 2003
Quite often you want to know who owns a given domain. To obtain the registry information, you go to the respective registry and start a so called WHOIS query (lookup). The trick is that you have to know which registry is responsible for which TLD (Top Level Domain).
 
Reader Level:

Quite often you want to know who owns a given domain. To obtain the registry information, you go to the respective registry and start a so-called WHOIS query (lookup). The trick is that you have to know which registry is responsible for which TLD (Top Level Domain).

The database is the so-called WHOIS database and it has one distinct property: it provides us with a query interface via TCP port 43! And as the .NET framework provides us with the TCPClient class, we can use this interface to directly obtain our data.

The following example is a minimal implementation of a WHOIS lookup (whois.aspx):

<% @Page Language="VB" %>
<% @Assembly Name="System.Net" %>
<% @Import Namespace="System.Net.Sockets" %>
<% @Import Namespace="System.Text" %>
<% @Import Namespace="System.IO" %>
<%
Dim tcpc As New TCPClient()
If 0 = tcpc.Connect("whois.networksolutions.com", 43) Then
Dim strDomain As [String] = "microsoft.com" + ControlChars.Cr + ControlChars.Lf
Dim arrDomain As [Byte]() = Encoding.ASCII.GetBytes(strDomain.ToCharArray())
Dim s As Stream = tcpc.GetStream()
s.Write(arrDomain, 0, strDomain.Length)
Dim sr As New StreamReader(tcpc.GetStream(), Encoding.ASCII)
While - 1 <> sr.Peek()
Response.Write((sr.ReadLine() + "<br>"))
End While
tcpc.Close()
Else
Response.Write("Could not connect to WHOIS server!")
End If
%>

To be able to work with the TCPClient class, we need the System.Net.Sockets namespace. We also need to add classes like System.Text or System.IO.

The TCPClient class is doing most of the work in this example: using it, I can connect to a server on any port (the Connect method does that) and send data to and fro (via Stream).

Sending the query warrants a little explanation: for sending I use the binary stream without any wrappers. Therefore, I have to turn the query string (CR/LF terminated) into a byte array (with ASCII encoding) and can then send the desired number of bytes to the server.

Fetching the data happens via the StreamReader, which allows comfortable and direct work with the character data. And to keep the example simple to the end, the received data are immediately passed through to the client - the result can be seen in the following screen shot.

share this article :
post comment
 

asdfsdfsdgdfgdfgdfhgdfdfhdgfh casa

Posted by Tavarua Roberts Nov 30, 2010
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor