Build a WHOIS Lookup in ASP.NET

Share this article

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 “rn” 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" + "rn"; 
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;  
objTCPC.Close();
The Code

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

  1. import System.Net.Sockets, System.Text, System.IO and System.Text.RegularExpressions namespaces in your ASP.NET page
  2. connect to WHOIS server using the TcpClient class
  3. send a domain query to the WHOIS server
  4. read the server response via StreamReader class
  5. replace the new line characters “n” in the server response with “<br>” using Regex class
  6. 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 http://www.mit.edu/afs/athena/contrib/potluck/Net-Services/whois-servers.list

Check out this fully functional example of an ASP.NET WHOIS page.

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)  
{  
 try  
 {  
   TcpClient objTCPC = new TcpClient(Request.Form["WhoisServer"], 43);  
   string strDomain = Request.Form["DomainName"] + "rn";  
   byte[] arrDomain = Encoding.ASCII.GetBytes(strDomain);  
     
   Stream objStream = objTCPC.GetStream();  
   objStream.Write(arrDomain, 0, strDomain.Length);  
   StreamReader objSR = new StreamReader(objTCPC.GetStream(),  
Encoding.ASCII);  
   lblResponse.Text = "<b>" + Request.Form["DomainName"] +  
"</b><br><br>" + Regex.Replace(objSR.ReadToEnd(),"n","<br>");  
         
   objTCPC.Close();  
 }  
 catch(Exception ex)  
 {  
   lblResponse.Text = ex.ToString();  
 }  
}  
 
</script>  
 
 
<html>  
<head>  
<style>  
.main {font-family:Verdana; font-size:12px;}  
.title {font-family:Verdana; font-size:18px; font-weight:bold;}  
</style>  
</head>  
<body>  
<span class="title" align="center">WHOIS ASP.NET page</span>  
 
<form method="POST" name="MainForm" runat="server">  
<table>  
 <tr>  
   <td class="main" align="right">Whois Server</td>  
   <td class="main">  
     <asp:DropDownList class="main" id="WhoisServer" runat="server">  
       <asp:ListItem value="whois.networksolutions.com">  
whois.networksolutions.com (.COM, .NET, .EDU)</asp:ListItem>  
       <asp:ListItem value="whois.ripe.net">whois.ripe.net  
(Europe)</asp:ListItem>  
       <asp:ListItem value="whois.cira.ca">whois.cira.ca (.CA)  
</asp:ListItem>  
       <asp:ListItem value="whois.nic.uk">whois.nic.uk  
(.CO.UK)</asp:ListItem>  
       <asp:ListItem value="whois.domain-registry.nl">  
whois.domain-registry.nl (.NL)</asp:ListItem>  
     </asp:DropDownList>  
   </td>  
 </tr>  
 <tr>  
   <td class="main" align="right">Domain Name:</td>  
   <td class="main"><input type="text" class="main"  
name="DomainName" value=""></td>  
 </tr>  
 <tr>  
   <td class="main">&nbsp;</td>  
   <td class="main"><input type="Submit" id="btnSubmit"  
OnServerClick="btn_Click" value="Send" runat="server" /></td>  
 </tr>  
</table>  
<br><br>  
<asp:Label class="main" id="lblResponse" runat="server"/>  
</form>  
</body>  
</html>

Frequently Asked Questions about WHOIS Lookup in ASP.NET

How can I implement a WHOIS lookup in ASP.NET?

Implementing a WHOIS lookup in ASP.NET involves creating a simple web application that sends a request to a WHOIS server and displays the response. This involves setting up a TCP client to connect to the WHOIS server, sending a domain name as a request, and reading the response. The response will contain information about the domain name, such as its registration status and the details of the domain registrar.

What is the purpose of a WHOIS lookup?

A WHOIS lookup is used to retrieve information about a domain name. This includes details about the domain’s registration status, the registrar’s details, and the domain’s creation and expiration dates. This information can be useful for various purposes, such as verifying the legitimacy of a website, investigating cybercrimes, or checking the availability of a domain name for registration.

Can I use WHOIS lookup to check the availability of a domain name?

Yes, you can use a WHOIS lookup to check the availability of a domain name. If the domain name is already registered, the WHOIS lookup will return information about the domain’s registration. If the domain name is not registered, the WHOIS lookup will not return any information, indicating that the domain name is available for registration.

How can I connect to a WHOIS server in ASP.NET?

In ASP.NET, you can connect to a WHOIS server using the TcpClient class. This class provides methods for connecting to a server over TCP, sending data to the server, and receiving data from the server. To connect to a WHOIS server, you need to create an instance of the TcpClient class, specify the WHOIS server’s address and port number, and call the Connect method.

How can I send a request to a WHOIS server in ASP.NET?

In ASP.NET, you can send a request to a WHOIS server using the NetworkStream class. This class provides methods for sending and receiving data over a network. To send a request to a WHOIS server, you need to create an instance of the NetworkStream class, specify the request data (the domain name), and call the Write method.

How can I read the response from a WHOIS server in ASP.NET?

In ASP.NET, you can read the response from a WHOIS server using the StreamReader class. This class provides methods for reading data from a stream. To read the response from a WHOIS server, you need to create an instance of the StreamReader class, specify the stream to read from (the network stream), and call the ReadToEnd method.

What information can I get from a WHOIS lookup?

A WHOIS lookup can provide various information about a domain name, including its registration status, the details of the domain registrar, and the domain’s creation and expiration dates. This information can be useful for various purposes, such as verifying the legitimacy of a website, investigating cybercrimes, or checking the availability of a domain name for registration.

Are there any limitations to using WHOIS lookup?

Yes, there are some limitations to using WHOIS lookup. For example, some domain registrars may limit the number of WHOIS lookups that can be performed in a certain period to prevent abuse. Also, some domain owners may choose to hide their information from WHOIS lookups for privacy reasons.

Can I use WHOIS lookup to find the owner of a domain name?

Yes, you can use a WHOIS lookup to find the owner of a domain name. However, this information may not always be available, as some domain owners choose to hide their information from WHOIS lookups for privacy reasons.

Is it possible to perform a WHOIS lookup for any domain name?

Yes, it is generally possible to perform a WHOIS lookup for any domain name. However, the information returned by the WHOIS lookup may vary depending on the domain’s registration status and the domain registrar’s policies.

Peter TodorovPeter Todorov
View Author

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.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form