Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vkbandi
Last active February 23, 2016 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vkbandi/ba57fe39738010595362 to your computer and use it in GitHub Desktop.
Save vkbandi/ba57fe39738010595362 to your computer and use it in GitHub Desktop.
A C#.net class to fetch whois information for a .com domain name
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);
}
}
}
}
@vkbandi
Copy link
Author

vkbandi commented Sep 17, 2015

More info about this code can be found in my blog post about getting Whois info in C#

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment