Article

Home » Server-side Coding » ASP & .NET Tutorials » Build a WHOIS Lookup in ASP.NET

About the Author

Peter Todorov

Peter runs a Windows hosting company www.ASP-Hosting.ca, which offers affordable ASP and ASP.NET hosting. He started recently www.ASPdev.org featuring ASP and ASP.NET articles and tutorials.

View all articles by Peter Todorov...

Build a WHOIS Lookup in ASP.NET

By Peter Todorov

March 25th, 2003

Reader Rating: 8.5

Page: 1 2 Next

I've heard this question hundreds of times: “How do I write a script that will check domain availability?”

Today I’ll show you how to implement so called WHOIS script in C# and ASP.NET.

About WHOIS

The WHOIS script is used to check domain name availability, or to find out who owns a particular domain name.

I bet you've already used a Web-based WHOIS lookup such as http://checkdomain.com

All you need to do is enter the domain name you want to check, and click the submit button. The result will show the domain registrant’s information if the domain is already registered, otherwise you’ll receive message saying that the domain is still available.

Obviously, whenever you make a WHOIS lookup, you query some kind of database where the registration information is kept. However, this WHOIS database is not centralized, so the actual domain records are generally not available from a single WHOIS server. The WHOIS servers for domain registration records are maintained by the organization authorized to register domain names.

In general, WHOIS servers accept connections via TCP on port 43, so we are able to communicate with the server if we use this port. For comprehensive list of WHOIS servers, visit http://www.mit.edu/afs/athena/contrib/potluck/Net-Services/whois-servers.list

Importing the Namespace

The .Net framework gives us the TcpClient class (System.Net.Sockets namespace), and with its help we are going to connect to the WHOIS server and request domain registration data directly.

First we need to import System.Net.Sockets, System.Text, System.IO and System.Text.RegularExpressions namespaces, because we’ll use them in our WHOIS ASP.NET page:

<% @Import Namespace="System.Net.Sockets" %>
<% @Import Namespace="System.Text" %>
<% @Import Namespace="System.IO" %>
<% @Import Namespace="System.Text.RegularExpressions" %>

Connecting to the Host

The TcpClient class provides methods for connecting, sending, and receiving data over a network. There are two ways to connect to remote host:

  1. Create an instance of the TcpClient class using no parameters, and then call one of the three TcpClient connect methods:

    TcpClient objTCPC = new TcpClient();
    objTCPC.Connect(“whois.networksolutions.com”, 43);

  2. Create an instance of the TcpClient class using the host name and port number of the server to which you want to connect. This constructor will automatically establish your connection:

    TcpClient objTCPC = new TcpClient
    (“whois.networksolutions.com”, 43);

Sending your Query

The next step, after we have successfully connected to the WHOIS server, is to send our domain query.

First we declare strDomain string, which will hold the domain name we want to look up. We concatenate “\r\n” to the end of our strDomain, because that is the format expected by the WHOIS servers. Finally we encode our strDomain into a byte array (arrDomain) using the GetBytes method of the Encoding class (System.Text namespace).

string strDomain = “domain-for-check.com” + "\r\n";
byte[] arrDomain = Encoding.ASCII.GetBytes(strDomain);

We will use the GetStream method to obtain a Stream (System.IO namespace) object that can send and receive data on the underlying connected socket:

Stream objStream = objTCPC.GetStream();

Once we have obtained access to the Stream object, we can write (send) a sequence of bytes to the current stream using the Write method:

objStream.Write(arrDomain, 0, strDomain.Length);

If you liked this article, share the love:
Print-Friendly Version Suggest an Article