whois查询,如何用C#实现一个Whois的查询

什么是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.zhenqiu.net 它是属于 .net后缀的,这个时候你就要去 whois.crnic.net这边来查询了。
接下来我们来看具体的实现代码。
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 { /// /// binbin /// public class Whois { /// /// diabled to new instance /// private Whois() { } /// /// Clear cache /// 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(); 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 serverList; /// /// .pro','.name', and '.tv' domains require an account for a whois /// ///
/// 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; } } }
我是把whois的服务器的文件放在一个文本文件里面 放在了 \App_Data\WhoisServer.txt这里面了。 这样在这个Whois类实例化的时候。就会自动去加载这些内容了。 关键的部分就是 Lookup方法了 Lookup允许传入的是域名,然后我们会去判断它是哪一个后缀,然后得到它是用哪一个server。接下来我们用 TcpClient去连接哪个server的43端口。把字符串变成字节流,发送到服务器,不断的读取服务器发送过来的内容 等到什么也读不到的时候就完成了这次查询,(这种是同步模式),然后把字节流变成字符串,就完成了这一个查询了。
看一下Demo是什么用它的。
新建一个WebForm page 在页面里面放一个 label控件取名为 lblResult。 哪么这个页面你就可以在浏览器里输入 http://yourserver:port/DomainWhois.aspx?domain=zhenqiu.net. 我在实际项目中用到的地址是 http://www.starteenwinkel.nl/domainwhois.aspx?domain=zhenqiu.net
public partial class DomainWhois : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string domain = Request.QueryString["Domain"]; if (!string.IsNullOrEmpty(domain)) { Whois whois = Whois.Create(Server.MapPath("~/App_Data/WhoisServer.txt")); lblResult.Text = whois.LookUp(domain).Replace("\r\n","
").Replace("\n","
"); } } }



Tags:  域名whois whois whois查询

延伸阅读

最新评论

发表评论