Skip to content
Advertisement

How to get whois information of a domain name in my program?

I want to get whois information of a domain name from my c#/java programs. Is there a simple way to do this?

Advertisement

Answer

I found a perfect C# example on dotnet-snippets.com (which doesn’t exist anymore).

It’s 11 lines of code to copy and paste straight into your own application.

/// <summary>
/// Gets the whois information.
/// </summary>
/// <param name="whoisServer">The whois server.</param>
/// <param name="url">The URL.</param>
/// <returns></returns>
private string GetWhoisInformation(string whoisServer, string url)
{
    StringBuilder stringBuilderResult = new StringBuilder();
    TcpClient tcpClinetWhois = new TcpClient(whoisServer, 43);
    NetworkStream networkStreamWhois = tcpClinetWhois.GetStream();
    BufferedStream bufferedStreamWhois = new BufferedStream(networkStreamWhois);
    StreamWriter streamWriter = new StreamWriter(bufferedStreamWhois);

    streamWriter.WriteLine(url);
    streamWriter.Flush();

    StreamReader streamReaderReceive = new StreamReader(bufferedStreamWhois);

    while (!streamReaderReceive.EndOfStream)
        stringBuilderResult.AppendLine(streamReaderReceive.ReadLine());

    return stringBuilderResult.ToString();
}
Advertisement