Domain name availability search using Whois information in C#


Programmatically searching for domain name availability is easier than I expected, all we have to do is get the Whois information for the domain name that we are looking for, and look for a specific text inside the Whois response.

I have already written a post on how to write a C# class to get whois information, you can go over there if you want more information on Whois, but if you just want the class name you can find it below and you can also find the complete source code at the links that I have provided.

Domain Search Code

public class DomainSearch
{
/// <summary>
/// Check whether a given domain name is available or not
/// </summary>
/// <param name="domainName">domain name to be verified</param>
/// <returns></returns>
public static bool IsDomainNameAvailable(string domainName)
{
string whoisData = Whois.Lookup(domainName);
string[] ws = whoisData.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
return ws[7].Contains("No match for domain \"" + domainName.ToUpper() + "\".");
}
}
view raw DomainSearch.cs hosted with ❤ by GitHub

Whois Information

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace DomainTools
{
/// <summary>
/// A class to lookup whois information.
/// </summary>
public class Whois
{
private const int Whois_Server_Default_PortNumber = 43;
private const string Domain_Record_Type = "domain";
private const string DotCom_Whois_Server = "whois.verisign-grs.com";
/// <summary>
/// Retrieves whois information
/// </summary>
/// <param name="domainName">The registrar or domain or name server whose whois information to be retrieved</param>
/// <param name="recordType">The type of record i.e a domain, nameserver or a registrar</param>
/// <returns></returns>
public static string Lookup(string domainName)
{
using (TcpClient whoisClient = new TcpClient())
{
whoisClient.Connect(DotCom_Whois_Server, Whois_Server_Default_PortNumber);
string domainQuery = Domain_Record_Type + " " + domainName + "\r\n";
byte[] domainQueryBytes = Encoding.ASCII.GetBytes(domainQuery.ToCharArray());
Stream whoisStream = whoisClient.GetStream();
whoisStream.Write(domainQueryBytes, 0, domainQueryBytes.Length);
StreamReader whoisStreamReader = new StreamReader(whoisClient.GetStream(), Encoding.ASCII);
string streamOutputContent = "";
List<string> whoisData = new List<string>();
while (null != (streamOutputContent = whoisStreamReader.ReadLine()))
{
whoisData.Add(streamOutputContent);
}
whoisClient.Close();
return String.Join(Environment.NewLine, whoisData);
}
}
}
}
view raw Whois.cs hosted with ❤ by GitHub

Usefull links

This post is originally published on coderbuddy.wordpress.com.

One thought on “Domain name availability search using Whois information in C#

Comments are closed.