A simple C# class to get whois information

I have searched on the web and found C# classes to obtain whois information, but none of those classes has addressed my issue of obtaining only the domain information, for example if i search for whois information of microsoft.com using the code available on the net you will also get all the name servers that have microsoft in the name.

None of the code i saw addressed the issue of excluding the name servers from the list and only obtaining the whois information for the domain name. So after a bit of research and coding this is what i got, i am sharing it with you. I will be very happy if it is of some use to anybody

using System;
using System.IO;
using System.Text;
using System.Net.Sockets;
using System.Collections.Generic;

namespace WebTools
{
    /// <summary>
    /// A class to lookup whois information.
    /// </summary>
    public class Whois
    {
        public enum RecordType { domain, nameserver, registrar };

        /// <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>The string containg the whois information</returns>
        public static string lookup(string domainname, RecordType recordType)
        {
            List<string> res = lookup(domainname, recordType, "whois.internic.net");
            string result = "";
            foreach (string st in res)
            {
                result += st + "\n";
            }
            return result;
        }        /// <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>
        /// <param name="returnlist">use "whois.internic.net" if you dont know whoisservers</param>
        /// <returns>The string list containg the whois information</returns>
        public static List<string> lookup(string domainname, RecordType recordType, string whois_server_address)
        {
            if (whois_server_address == "")
                whois_server_address = "whois.internic.net";
            TcpClient tcp = new TcpClient();
            tcp.Connect(whois_server_address, 43);
            string strDomain = recordType.ToString() + " " + domainname + "\r\n";
            byte[] bytDomain = Encoding.ASCII.GetBytes(strDomain.ToCharArray());
            Stream s = tcp.GetStream();
            s.Write(bytDomain, 0, strDomain.Length);
            StreamReader sr = new StreamReader(tcp.GetStream(), Encoding.ASCII);
            string strLine = "";
            List<string> result = new List<string>();
            while (null != (strLine = sr.ReadLine()))
            {
                result.Add(strLine);
            }
            tcp.Close();
            return result;
        }
    }
}

You can use obtain the whois information by simple using the following code

string info = Whois.lookup("google.com", Whois.RecordType.domain);

or you can alternatively use

List<string> info = Whois.lookup("google.com", Whois.RecordType.domain, "whois.internic.net");

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

I will try and post a entry about whois queries, for those who are interested go to coderbuddy Home


Update

Click the download button bellow to download the complete source code (Visual studio 2005, C#.net, Windows Class Library Project) including the C# class to search for domain name search using whois information.

Download button
Related Articles

Advertisement

6 Comments on “A simple C# class to get whois information”

  1. [...] the original here: A simple C# class to get whois information « Coder buddy By admin | category: domain name lookup | tags: code, domain-name, from-the-list, issue, [...]

  2. [...] posted here:  A simple C# class to get whois information « Coder buddy By admin | category: domain lookup | tags: address, conversion, dial-up, domain lookup, [...]

  3. Sigorny says:

    cool code man – fair play – its well useful!

  4. [...] A simple C# class to get whois information (coderbuddy.wordpress.com) .dk whois, dk hostmaster whois, Domain name registry, Domain privacy, dot dk whois, ICANN, WHOIS [...]

  5. sa says:

    it doesnt work for many websites,
    thanks anyway

    • Vamsi says:

      It only works for .com TLD, if you want to make it work for other TLD’s change the whois_server_address value, you can find a list of whois servers for popular TLD’s here http://www.domaininformation.de/whoisserver_list.html


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.