Last active
February 23, 2016 19:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More info about this code can be found in my blog post about getting Whois info in C#