www.softwareriviera.com
Software Code
    New Code     Most Interesting     Don't Miss     Our picks  
C# - section

View Article
C# WhoIs Server Query
C# Query WhoIs Server example - This is an example for querying a WhoIs Server for a domain name.
The WHOIS database provides an interface through TCP port 43. We can connect to this interface for querying the database which of course saves us the effort of scraping the web pages at some web site.

Bellow you will find a function which queryies a WhoIs Server for a given domain name:

The function accepts:
- a domain name as string (such as tucows.com, google.com, etc.com)
- and a WhoIs server as string (such as whois.tucows.com, etc)

The result is
- null if there was an error (because of implemented error handling procedure)
or
- the actual response of the WhoIs server for the given domain name
        public string QueryDomainForWhoIsServer(string domain, string whoisServer)
{
System.Threading.Thread.Sleep(1000);//Failsafe: Extra Safety Measure
int ccStart = domain.LastIndexOf(".");
string ret = null;
Socket s = null;
try
{
string cc = domain.Substring(ccStart + 1);
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPHostEntry myHostEntry = Dns.GetHostEntry(whoisServer);

s.Connect(new IPEndPoint(myHostEntry.AddressList[0], 43));
s.Send(Encoding.ASCII.GetBytes(domain + "\r\n"));
byte[] buffer = new byte[1024];
int recv = s.Receive(buffer);
while (recv > 0)
{
ret += Encoding.ASCII.GetString(buffer, 0, recv);
recv = s.Receive(buffer);
}
s.Shutdown(SocketShutdown.Both);
}
catch (Exception ex)
{
//HANDLE ERROR
//WRAP ex.StackTrace + " " + ex.Message in an object and return it
return null;
}
finally
{
if (s != null)
s.Close();
}
return ret;
}
Date: 2007-12-6    Written by: Admin A
     See all articles in this section
Copyright © 2007-2009 SoftwareRiviera.com. All rights reserved.  | Terms of use | Disclaimer | Privacy Policy |