How to use C# to find domains

Recently I was searching for domain names out on the interwebs and realized there was no good way (I could find) to search for possible domain names in bulk. For example, I wanted to type in “nathan” into some awesome app and have it tell me all of the domain extensions (TLDs) that were available for purchase. So, I threw together a C# app just for that purpose and wanted to share the process here.

Step 1

In order to check for a domain’s availability, we’ll need to make two calls out to the internet. First, we’ll want to grab the whois server that is used with the TLD you’re searching for (.com, .coffee, .etc). This server is responsible for telling us who, if anyone, has the domain name registered. After we retrieve the whois server to use, we’ll make a second call out to that server to check for registration on the domain we’re searching for.

Now, for some code. The first thing we’re going to do is create a method that will make a request out to our whois server and return the result.

private static string GetWhoisInformation(string whoisServer, string query)
{
    StringBuilder ret = new StringBuilder();

    using (TcpClient whoisClient = new TcpClient(whoisServer, 43))
    using (NetworkStream networkStream = whoisClient.GetStream())
    using (BufferedStream bufferedStream = new BufferedStream(networkStream))
    {
        StreamWriter streamWriter = new StreamWriter(bufferedStream);
        streamWriter.WriteLine(query);
        streamWriter.Flush();

        StreamReader streamReader = new StreamReader(bufferedStream);

        while (!streamReader.EndOfStream)
            ret.AppendLine(streamReader.ReadLine());
    }

    return ret.ToString();
}

The main takeaway from this method is that we’re going to open a TCP connection to our whois server, request information about our query, and then read the results back.

Step 2

Now that the core method is set up, we’ll want to stash the root whois server in a variable. This server will be the one responsible for telling us which whois server we should use to check for the domain itself.

private const string RootTLDServerURL = @"whois.iana.org";

Step 3

Next, we’ll set up our initial method that we can call to check for a domain names’ availability. This method will take two parameters: a keyword and a TLD.

public static bool IsAvailable(string keyword, string tld)
{
}

Step 4

As stated above, the first thing we need to do is retrieve the whois server that is associated with the TLD that we are searching for. Add the following code into our IsAvailable method:

// Search IANA to find the whois server for the given TLD
string whoisForRoot = GetWhoisInformation(RootTLDServerURL, tld);
whoisForRoot = whoisForRoot.Remove(0, whoisForRoot.IndexOf("whois:", StringComparison.Ordinal) + 6).TrimStart();

If we were to query our root whois server with .coffee, the results would look like this:

whois.donuts.co

status:       ACTIVE
remarks:      Registration information: http://donuts.co

created:      2013-12-19
changed:      2013-12-28
source:       IANA

If you look back at the code we have, the only thing we care about is that first line. This is the whois server URL that we are going to want for our next call.

Step 5

Next, we’re going to make a call out to the whois server retrieved above and check for a registration for the domain we’re trying to check. Add this code into our IsAvailable method to do so:

// Search the resulting whois server to check domain availability
string tldServer = whoisForRoot.Substring(0, whoisForRoot.IndexOf('\r'));    
string domain = string.Format("{0}.{1}", keyword, tld);
string whois = GetWhoisInformation(tldServer, domain);

Now, if we were to query for nathanloves.coffee, we would get some results back that look like this (at the time of writing):

Domain not found.

Terms of Use: Users accessing the Donuts WHOIS service must agree to use the data only for lawful purposes, and under under no circumstances use the data to: Allow, enable, or otherwise support the transmission by e-mail, telephone, or facsimile of mass unsolicited, commercial advertising or solicitations to entities other than the registrar's own existing customers. Enable high volume, automated, electronic processes that send queries or data to the systems of Donuts or any ICANN-accredited registrar, except as reasonably necessary to register domain names or modify existing registrations. When using the Donuts Whois service, please consider the following: The Whois service is not a replacement for standard EPP commands to the SRS service. Whois is not considered authoritative for registered domain objects. The Whois service may be scheduled for downtime during production or OT&E maintenance periods. Queries to the Whois services are throttled. If too many queries are received from a single IP address within a specified time, the service will begin to reject further queries for a period of time to prevent disruption of Whois service access.
Perfect! That’s exactly what we want to see. On the other hand, if the domain is registered, you would be looking at a whole bunch of registration information, such as the administrative contact, the technical contact, and so on.

You’re done!

Throwing this all together, we end up with this:

public static bool IsAvailable(string keyword, string tld)
{
    // Search IANA to find the whois server for the given TLD
    string whoisForRoot = GetWhoisInformation(RootTLDServerURL, tld);
    whoisForRoot = whoisForRoot.Remove(0, whoisForRoot.IndexOf("whois:", StringComparison.Ordinal) + 6).TrimStart();

    // Search the resulting whois server to check domain availability
    string tldServer = whoisForRoot.Substring(0, whoisForRoot.IndexOf('\r'));
    string domain = string.Format("{0}.{1}", keyword, tld);
    string whois = GetWhoisInformation(tldServer, domain);

    return whois.Contains("Domain not found") || whois.Contains("No match for");
}

Pretty easy right? Now to use this, all you have to do is call it like so:

bool result = IsAvailable("nathanloves", "coffee");