zoukankan      html  css  js  c++  java
  • 如何用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
    {
        
    /// <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<stringstring>();
                            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<stringstring> 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;
            }
        }
    }

     我是把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","<br />").Replace("\n","<br />");
            }
        }
    }


     
  • 相关阅读:
    linux分区
    MySQL
    RGB中的颜色的设置
    解决UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 12: ordinal not in range(128)的编码问题
    解决运行scrapy是报错No module named cryptography,解决cryptography的安装问题,解决libffi的安装问题
    解决pycharm下安装reportLab报错的问题
    简单的爬取并下载图片的程序
    linux常用命令
    ubuntu下安装pycharm的方法
    win7下装ubuntu双系统后无法进入win7的解决方法
  • 原文地址:https://www.cnblogs.com/lovebanyi/p/2182314.html
Copyright © 2011-2022 走看看