C # Implementation of a WHOIS query

Source: Internet
Author: User

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
These are the WHOIS servers I've collected.


For example, you want to query the domain name is www.111cn.net it belongs to the. NET suffix, this time you will go to whois.crnic.net this side to query.

Next we look at the specific implementation code.

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;
}
}
}

I put the WHOIS server file in a text file and put it in the

App_datawhoisserver.txt, it's inside. This is when the Whois class is instantiated. It will automatically load the content.
The key part is the lookup method, where lookup allows for a domain name to be passed in, and then we'll determine which suffix it is and what server it is. Next we use
TcpClient to connect to which server's 43 port. The string into a byte stream, sent to the server, constantly read the server sent over the content to wait until nothing can be read to complete the query, (this is synchronous mode), and then the word stream into a string, the completion of this query.

Take a look at what the demo is for.


Create a new WebForm page place a label control in the pages named Lblresult.
This page you can enter http://yourserver:port/DomainWhois.asp tutorial x?domain=zhenqiu.net in the browser.
The address I used in the actual project is

Http://www.starteenwinkel.nl/domainwhois.aspx?domain=zhenqiu.net

 

Public Partial class DomainWhois:System.Web.UI.Page
{
    protected void Page_Load (object sender, Event Args 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", "<br/>"). Replace ("n", "<br/>");
       }
   }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.