ASP Hosting sale!
1,000MB space, 40GB transfer, 1 SQL Server 2000 db! - $9.95 per month*!
Limited Time Offer!

aspdev | articles | tutorials | forums

ASPdev -> All Articles -> Build a WHOIS Lookup in ASP.NET

Build a WHOIS Lookup in ASP.NET

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.


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

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

Importing the .NET Namespaces

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:

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(“”, 43);

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
(“”, 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 = “” + "\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);

Getting a Response

One of the ways to get the WHOIS server response is to create an instance of the StreamReader class (System.IO namespace) that reads characters from a byte stream in a particular encoding:

StreamReader objSr = new StreamReader
(objTCPC.GetStream(), Encoding.ASCII);

The next step is to use ReadToEnd method of our objSr object, to obtain the WHOIS server response:

string strServerResponse = objSr.ReadToEnd();

Because we’ve read the server response at once, we need to replace the new line characters “\n” in our text response with “<br>” in order to be displayed correctly in the browser. We can do this with the help of the Regex class (System.Text.RegularExpressions) and particularly with its Replace method:

strServerResponse = Regex.Replace
(strServerResponse, "\n", "<br>");

The only thing left to do after we receive the server response is to assign the strServerResponse value to lblResponse.Text property (lblResponse is our label server control where the result will be displayed), and close the TCP connection to the WHOIS server:

lblResponse.Text = strServerResponse;

The Code

In summary, to create a WHOIS lookup ASP.NET page you need to:

. import System.Net.Sockets, System.Text, System.IO and System.Text.RegularExpressions namespaces in your ASP.NET page

. connect to WHOIS server using the TcpClient class

. send a domain query to the WHOIS server

. read the server response via StreamReader class

. replace the new line characters “\n” in the server response with “<br>” using Regex class

. display the response string

Here’s the fully functional code for WHOIS ASP.NET page. Please keep in mind that the WHOIS servers used in the example can be changed to any valid WHOIS servers for reference, check

Here's the page's code:

<% @Page Language="C#" Debug="false"%>
<% @Import Namespace="System.Net.Sockets" %>
<% @Import Namespace="System.Text" %>
<% @Import Namespace="System.IO" %>
<% @Import Namespace="System.Text.RegularExpressions" %>

<script language="C#" runat=server>

public void btn_Click(object sender, EventArgs eArgs)
TcpClient objTCPC = new TcpClient(Request.Form["WhoisServer"], 43);
string strDomain = Request.Form["DomainName"] + "\r\n";
byte[] arrDomain = Encoding.ASCII.GetBytes(strDomain);

Stream objStream = objTCPC.GetStream();
objStream.Write(arrDomain, 0, strDomain.Length);
StreamReader objSR = new StreamReader(objTCPC.GetStream(),
lblResponse.Text = "<b>" + Request.Form["DomainName"] +
"</b><br><br>" + Regex.Replace(objSR.ReadToEnd(),"\n","<br>");

catch(Exception ex)
lblResponse.Text = ex.ToString();


.main {font-family:Verdana; font-size:12px;}
.title {font-family:Verdana; font-size:18px; font-weight:bold;}
<span class="title" align="center">WHOIS ASP.NET page</span>

<form method="POST" name="MainForm" runat="server">
<td class="main" align="right">Whois Server</td>
<td class="main">
<asp:DropDownList class="main" id="WhoisServer" runat="server">
<asp:ListItem value=""> (.COM, .NET, .EDU)</asp:ListItem>
<asp:ListItem value="">
<asp:ListItem value=""> (.CA)
<asp:ListItem value="">
<asp:ListItem value=""> (.NL)</asp:ListItem>
<td class="main" align="right">Domain Name:</td>
<td class="main"><input type="text" class="main"
name="DomainName" value=""></td>
<td class="main"> </td>
<td class="main"><input type="Submit" id="btnSubmit"
OnServerClick="btn_Click" value="Send" runat="server" /></td>
<asp:Label class="main" id="lblResponse" runat="server"/>

Contact Us