Tommy Cawley
Tommy Cawley

Reputation: 185

Dns NameServer Query C#

I am trying to query a web address and get current non cached results like the root name server and the administrative contact email. Could ye please point me to a guide with sample code on how this can be achieved. Thanks Tommy

Upvotes: 0

Views: 806

Answers (1)

user3766626
user3766626

Reputation:

It looks like you needs WhoIs lookup. DNS lookup does not return administrative email. DNS Lookup returns DNS records such as A, CNAME, MX and TXT records.

For whois query, you will need to query WhoIs data from whois server that the domain belongs to. The following code snippet uses NetworkStream to read WhoIs data of a .com domain:

// Create new socket object
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
string query = "mydomain.com";
NetworkStream nst;

try
{
    IPEndPoint endPoint = new IPEndPoint("whois.internic.net", 43)
    socket.Connect(endPoint);

    nst = new NetworkStream(socket, true);

    string str;
    StreamWriter writer = new StreamWriter(nst);
    writer.WriteLine(query);
    writer.Flush();

    StringBuilder builder = new StringBuilder();
    StreamReader reader = new StreamReader(nst);
    while ((str = reader.ReadLine()) != null)
    {
        builder.Append(str);
        builder.Append(
#if !NETCF
            Environment.NewLine
#else
            "\r\n"                        
#endif
            );
    }
    result = builder.ToString();
}
finally
{
    if (nst != null)
        nst.Close();

    socket.Close();
}

Upvotes: 1

Related Questions