一聚教程网:一个值得你收藏的教程网站

热门教程

C#实现一个Whois的查询

时间:2022-06-25 08:25:48 编辑:袖梨 来源:一聚教程网

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.111com.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_DataWhoisServer.txt这里面了。 这样在这个Whois类实例化的时候。就会自动去加载这些内容了。
关键的部分就是 Lookup方法了 Lookup允许传入的是域名,然后我们会去判断它是哪一个后缀,然后得到它是用哪一个server。接下来我们用
TcpClient去连接哪个server的43端口。把字符串变成字节流,发送到服务器,不断的读取服务器发送过来的内容 等到什么也读不到的时候就完成了这次查询,(这种是同步模式),然后把字节流变成字符串,就完成了这一个查询了。
 
看一下Demo是什么用它的。


新建一个WebForm page 在页面里面放一个 label控件取名为 lblResult。
哪么这个页面你就可以在浏览器里输入 http://yourserver:port/DomainWhois.asp教程x?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("rn","
").Replace("n","
");
        }
    }
}

热门栏目