什么是whois
简单来说,whois就是一个用来查询域名是否已经被注册,以及注册域名的详细信息的数据库(如域名所有人、域名注册商、域名注册日期和过期日期等)。通过whois来实现对域名信息的查询
什么去查询Whois?
Whois的查询其实也是蛮简单的,就是利用Socket去连接whois提供的服务器。Whois服务的默认端口是43,查询的话就是把域名往这边发送过去,Whois服务器在收到你的请求后就会返回纯文本的格式,这个写起来真的蛮容易,比查询dns协议简单多了。
Whois的服务器有哪些呢?
nl whois.domain-registry.nl
eu whois.eu
edu whois.educause.net
net whois.crsnic.net
com whois.crsnic.net
org whois.crsnic.net
info whois.afilias.com
de whois.denic.de
cn whois.cnnic.net.cn
这些是我收集的whois服务器
比如你要查询的域名 是www.17aspx.com它是属于.com后缀的,这个时候你就要去 whois.crnic.com这边来查询了。
接下来我们来看具体的实现代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace Qianfa.Utility
{
/// <summary>
/// <author>binbin</author>
/// </summary>
public class Whois
{
/// <summary>
/// diabled to new instance
/// </summary>
private Whois()
{
}
/// <summary>
/// Clear cache
/// </summary>
public static void ClearCache()
{
lock (_lock)
{
_instance = null;
}
}
private static Whois _instance =null;
private static object _lock = new object();
public static Whois Create(string path)
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
_instance = new Whois();
_instance.serverList = new Dictionary<string, string>();
StreamReader sr = new StreamReader(path);
while (sr.Peek() != -1)
{
string line = sr.ReadLine();
string[] temp = line.Split(new char[] { ''\t'', '' '' });
_instance.serverList.Add(temp[0].ToLower(), temp[1]);
}
}
}
}
return _instance;
}
public Dictionary<string, string> serverList;
/// <summary>
/// .pro'',''.name'', and ''.tv'' domains require an account for a whois
/// </summary>
/// <param name="domain"></param>
/// <returns></returns>
public string LookUp(string domain)
{
string result = "";
string[] temp = domain.Split(''.'');
string suffix = temp[temp.Length - 1].ToLower();// get the last;
if (!serverList.Keys.Contains(suffix))
{
result= string.Format(Resources.Whois.NoSupport,suffix);
return result;
}
string server = serverList[suffix];
TcpClient client = new TcpClient();
NetworkStream ns;
try
{
client.Connect(server, 43);
ns = client.GetStream();
byte[] buffer = Encoding.ASCII.GetBytes(domain + "\rn");
ns.Write(buffer, 0, buffer.Length);
buffer = new byte[8192];
int i = ns.Read(buffer, 0, buffer.Length);
while (i > 0)
{
Encoding encoding = Encoding.UTF8;
//if (suffix == "cn")
//{
// encoding = Encoding.UTF8;
//}
//else
//{
// encoding = ASCIIEncoding.ASCII;
//}
result += encoding.GetString(buffer, 0, i);
i = ns.Read(buffer, 0, buffer.Length);
}
}
catch(SocketException)
{
result = Resources.Whois.SocketError;
return result;
}
ns.Close();
client.Close();
return result;
}
}
}
|