[C#]域名的Whois信息查询
2010-01-20 16:38
375 查看
研究了一天,终于可以拿出来晒晒了。
刚开始,我一直用:whois.crsnic.net 查询 .com的域名,可查询几次后发现,很多查不到,于是仔细读了一下返回的信息,
Domain Name: FUTE53.COM
Registrar: XIN NET TECHNOLOGY CORPORATION
Whois Server: whois.paycenter.com.cn
Referral URL: http://www.xinnet.com Name Server: NS1.MYHOSTADMIN.NET
Name Server: NS2.MYHOSTADMIN.NET
Status: ok
Updated Date: 22-mar-2009
Creation Date: 22-mar-2009
Expiration Date: 22-mar-2010
>>> Last update of whois database: Wed, 20 Jan 2010 07:10:47 UTC <<<
NOTICE: The expiration date displayed in this record is the date the
registrar's sponsorship of the domain name registration in the registry is
currently set to expire. This date does not necessarily reflect the expiration
date of the domain name registrant's agreement with the sponsoring
registrar. Users may consult the sponsoring registrar's Whois database to
view the registrar's reported date of expiration for this registration.
TERMS OF USE: You are not authorized to access or query our Whois
database through the use of electronic processes that are high-volume and
automated except as reasonably necessary to register domain names or
modify existing registrations; the Data in VeriSign Global Registry
Services' ("VeriSign") Whois database is provided by VeriSign for
information purposes only, and to assist persons in obtaining information
about or related to a domain name registration record. VeriSign does not
guarantee its accuracy. By submitting a Whois query, you agree to abide
by the following terms of use: You agree that you may use this Data only
for lawful purposes and that under no circumstances will you use this Data
to: (1) allow, enable, or otherwise support the transmission of mass
unsolicited, commercial advertising or solicitations via e-mail, telephone,
or facsimile; or (2) enable high volume, automated, electronic processes
that apply to VeriSign (or its computer systems). The compilation,
repackaging, dissemination or other use of this Data is expressly
prohibited without the prior written consent of VeriSign. You agree not to
use electronic processes that are automated and high-volume to access or
query the Whois database except as reasonably necessary to register
domain names or modify existing registrations. VeriSign reserves the right
to restrict your access to the Whois database in its sole discretion to ensure
operational stability. VeriSign may restrict or terminate your access to the
Whois database for failure to abide by these terms of use. VeriSign
reserves the right to modify these terms at any time.
The Registry database contains ONLY .COM, .NET, .EDU domains and
Registrars.
Domain Name: FUTE53.COM
Registrar: XIN NET TECHNOLOGY CORPORATION
Whois Server: whois.paycenter.com.cn
Referral URL: http://www.xinnet.com Name Server: NS1.MYHOSTADMIN.NET
Name Server: NS2.MYHOSTADMIN.NET
Status: ok
Updated Date: 22-mar-2009
Creation Date: 22-mar-2009
Expiration Date: 22-mar-2010
>>> Last update of whois database: Wed, 20 Jan 2010 07:10:47 UTC <<<
NOTICE: The expiration date displayed in this record is the date the
registrar's sponsorship of the domain name registration in the registry is
currently set to expire. This date does not necessarily reflect the expiration
date of the domain name registrant's agreement with the sponsoring
registrar. Users may consult the sponsoring registrar's Whois database to
view the registrar's reported date of expiration for this registration.
TERMS OF USE: You are not authorized to access or query our Whois
database through the use of electronic processes that are high-volume and
automated except as reasonably necessary to register domain names or
modify existing registrations; the Data in VeriSign Global Registry
Services' ("VeriSign") Whois database is provided by VeriSign for
information purposes only, and to assist persons in obtaining information
about or related to a domain name registration record. VeriSign does not
guarantee its accuracy. By submitting a Whois query, you agree to abide
by the following terms of use: You agree that you may use this Data only
for lawful purposes and that under no circumstances will you use this Data
to: (1) allow, enable, or otherwise support the transmission of mass
unsolicited, commercial advertising or solicitations via e-mail, telephone,
or facsimile; or (2) enable high volume, automated, electronic processes
that apply to VeriSign (or its computer systems). The compilation,
repackaging, dissemination or other use of this Data is expressly
prohibited without the prior written consent of VeriSign. You agree not to
use electronic processes that are automated and high-volume to access or
query the Whois database except as reasonably necessary to register
domain names or modify existing registrations. VeriSign reserves the right
to restrict your access to the Whois database in its sole discretion to ensure
operational stability. VeriSign may restrict or terminate your access to the
Whois database for failure to abide by these terms of use. VeriSign
reserves the right to modify these terms at any time.
The Registry database contains ONLY .COM, .NET, .EDU domains and
Registrars.
原来返回的信息里面给出了whois server ,连接试试,发现可用,于是就想到了利用正则的方式求正确whois所在server,然后再求whois info.
/*
HttpContext.Current.Response.Write(Whois.GetWhoisInfo("fute53.com"));
*/
public class Whois
{
public static string GetWhoisInfo(string strDomain)
{
try
{
string result = "";
string server = "whois.crsnic.net";
string domain = GetDomain(strDomain);
if (domain == null)
{
return "Please enter a valid domain name.";
}
else
{
string end = domain.Substring(domain.LastIndexOf("."));
switch (end)
{
case ".biz":
case ".info":
case ".mil":
server = "whois.networksolutions.com";
break;
case ".cn":
server = "whois.cnnic.net.cn";
break;
case ".edu":
server = "whois.educause.net";
break;
case ".org":
server = "whois.publicinterestregistry.net";
break;
case ".gov":
server = "whois.nic.gov";
break;
case ".hk":
server = "whois.hkdnr.net.hk";
break;
case ".name":
server = "whois.nic.name";
break;
default:
server = "whois.crsnic.net";
break;
}
result = GetWhoisInfo(server, domain);
if (result.IndexOf("no data found") >= 0 || result.IndexOf("No match for \"" + domain.ToUpper() + "\".") > 0)
{
result = GetWhoisInfo("Whois.internic.net", domain);
}
Regex reg = new Regex(@"Whois\s?Server\s?\:\s?(.*?)[\n|\r]", RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match mt = reg.Match(result);
if (String.IsNullOrEmpty(mt.Value))
{
return result;
}
else
{
string trueService = mt.Groups[1].Value.Trim();
if (trueService == server) return result;
else return GetWhoisInfo(trueService, domain);
}
}
}
catch (Exception e)
{
return e.Message;
}
}
public static string GetWhoisInfo(string server, string strDomain)
{
strDomain = GetDomain(strDomain);
if (strDomain == null) return "Domain is null or error!";
string result = "";
UTF8Encoding utf8 = new UTF8Encoding();
// Connect to the whois server
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(server, 43);
NetworkStream networkStream = tcpClient.GetStream();
// Send the domain name to the whois server
strDomain = strDomain.Replace("0x00", "");
byte[] buffer = Encoding.GetEncoding("GB2312").GetBytes(strDomain + "\r\n");
//byte[] buffer = utf8.GetBytes(domain + "\r\n");
networkStream.Write(buffer, 0, buffer.Length);
// Read back the results
buffer = new byte[10240];
int i = networkStream.Read(buffer, 0, buffer.Length);
while (i > 0)
{
i = networkStream.Read(buffer, 0, buffer.Length);
result += utf8.GetString(buffer);
//result +=Encoding.GetEncoding("GB2312").GetString(buffer);
}
networkStream.Close();
tcpClient.Close();
//return the lookup resutlt
result = result.Replace("\u0000", "");
return result;
}
public static string GetDomain(string strDomain)
{
if (String.IsNullOrEmpty(strDomain)) return null;
string domain = strDomain.Trim().ToLower();
if (domain.StartsWith("www."))
{
return domain.Substring(4, domain.Length - 4);
}
if (domain.IndexOf(".") < 0) domain += ".com";
if (domain == "" || domain.IndexOf(",") != -1 || domain.IndexOf(";") != -1 || domain.IndexOf("_") != -1 || domain.IndexOf(".") == -1)
{
return null;
}
return domain;
}
}
时间有限,不多写了,希望大家多给给意见
刚开始,我一直用:whois.crsnic.net 查询 .com的域名,可查询几次后发现,很多查不到,于是仔细读了一下返回的信息,
Domain Name: FUTE53.COM
Registrar: XIN NET TECHNOLOGY CORPORATION
Whois Server: whois.paycenter.com.cn
Referral URL: http://www.xinnet.com Name Server: NS1.MYHOSTADMIN.NET
Name Server: NS2.MYHOSTADMIN.NET
Status: ok
Updated Date: 22-mar-2009
Creation Date: 22-mar-2009
Expiration Date: 22-mar-2010
>>> Last update of whois database: Wed, 20 Jan 2010 07:10:47 UTC <<<
NOTICE: The expiration date displayed in this record is the date the
registrar's sponsorship of the domain name registration in the registry is
currently set to expire. This date does not necessarily reflect the expiration
date of the domain name registrant's agreement with the sponsoring
registrar. Users may consult the sponsoring registrar's Whois database to
view the registrar's reported date of expiration for this registration.
TERMS OF USE: You are not authorized to access or query our Whois
database through the use of electronic processes that are high-volume and
automated except as reasonably necessary to register domain names or
modify existing registrations; the Data in VeriSign Global Registry
Services' ("VeriSign") Whois database is provided by VeriSign for
information purposes only, and to assist persons in obtaining information
about or related to a domain name registration record. VeriSign does not
guarantee its accuracy. By submitting a Whois query, you agree to abide
by the following terms of use: You agree that you may use this Data only
for lawful purposes and that under no circumstances will you use this Data
to: (1) allow, enable, or otherwise support the transmission of mass
unsolicited, commercial advertising or solicitations via e-mail, telephone,
or facsimile; or (2) enable high volume, automated, electronic processes
that apply to VeriSign (or its computer systems). The compilation,
repackaging, dissemination or other use of this Data is expressly
prohibited without the prior written consent of VeriSign. You agree not to
use electronic processes that are automated and high-volume to access or
query the Whois database except as reasonably necessary to register
domain names or modify existing registrations. VeriSign reserves the right
to restrict your access to the Whois database in its sole discretion to ensure
operational stability. VeriSign may restrict or terminate your access to the
Whois database for failure to abide by these terms of use. VeriSign
reserves the right to modify these terms at any time.
The Registry database contains ONLY .COM, .NET, .EDU domains and
Registrars.
Domain Name: FUTE53.COM
Registrar: XIN NET TECHNOLOGY CORPORATION
Whois Server: whois.paycenter.com.cn
Referral URL: http://www.xinnet.com Name Server: NS1.MYHOSTADMIN.NET
Name Server: NS2.MYHOSTADMIN.NET
Status: ok
Updated Date: 22-mar-2009
Creation Date: 22-mar-2009
Expiration Date: 22-mar-2010
>>> Last update of whois database: Wed, 20 Jan 2010 07:10:47 UTC <<<
NOTICE: The expiration date displayed in this record is the date the
registrar's sponsorship of the domain name registration in the registry is
currently set to expire. This date does not necessarily reflect the expiration
date of the domain name registrant's agreement with the sponsoring
registrar. Users may consult the sponsoring registrar's Whois database to
view the registrar's reported date of expiration for this registration.
TERMS OF USE: You are not authorized to access or query our Whois
database through the use of electronic processes that are high-volume and
automated except as reasonably necessary to register domain names or
modify existing registrations; the Data in VeriSign Global Registry
Services' ("VeriSign") Whois database is provided by VeriSign for
information purposes only, and to assist persons in obtaining information
about or related to a domain name registration record. VeriSign does not
guarantee its accuracy. By submitting a Whois query, you agree to abide
by the following terms of use: You agree that you may use this Data only
for lawful purposes and that under no circumstances will you use this Data
to: (1) allow, enable, or otherwise support the transmission of mass
unsolicited, commercial advertising or solicitations via e-mail, telephone,
or facsimile; or (2) enable high volume, automated, electronic processes
that apply to VeriSign (or its computer systems). The compilation,
repackaging, dissemination or other use of this Data is expressly
prohibited without the prior written consent of VeriSign. You agree not to
use electronic processes that are automated and high-volume to access or
query the Whois database except as reasonably necessary to register
domain names or modify existing registrations. VeriSign reserves the right
to restrict your access to the Whois database in its sole discretion to ensure
operational stability. VeriSign may restrict or terminate your access to the
Whois database for failure to abide by these terms of use. VeriSign
reserves the right to modify these terms at any time.
The Registry database contains ONLY .COM, .NET, .EDU domains and
Registrars.
原来返回的信息里面给出了whois server ,连接试试,发现可用,于是就想到了利用正则的方式求正确whois所在server,然后再求whois info.
/*
HttpContext.Current.Response.Write(Whois.GetWhoisInfo("fute53.com"));
*/
public class Whois
{
public static string GetWhoisInfo(string strDomain)
{
try
{
string result = "";
string server = "whois.crsnic.net";
string domain = GetDomain(strDomain);
if (domain == null)
{
return "Please enter a valid domain name.";
}
else
{
string end = domain.Substring(domain.LastIndexOf("."));
switch (end)
{
case ".biz":
case ".info":
case ".mil":
server = "whois.networksolutions.com";
break;
case ".cn":
server = "whois.cnnic.net.cn";
break;
case ".edu":
server = "whois.educause.net";
break;
case ".org":
server = "whois.publicinterestregistry.net";
break;
case ".gov":
server = "whois.nic.gov";
break;
case ".hk":
server = "whois.hkdnr.net.hk";
break;
case ".name":
server = "whois.nic.name";
break;
default:
server = "whois.crsnic.net";
break;
}
result = GetWhoisInfo(server, domain);
if (result.IndexOf("no data found") >= 0 || result.IndexOf("No match for \"" + domain.ToUpper() + "\".") > 0)
{
result = GetWhoisInfo("Whois.internic.net", domain);
}
Regex reg = new Regex(@"Whois\s?Server\s?\:\s?(.*?)[\n|\r]", RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match mt = reg.Match(result);
if (String.IsNullOrEmpty(mt.Value))
{
return result;
}
else
{
string trueService = mt.Groups[1].Value.Trim();
if (trueService == server) return result;
else return GetWhoisInfo(trueService, domain);
}
}
}
catch (Exception e)
{
return e.Message;
}
}
public static string GetWhoisInfo(string server, string strDomain)
{
strDomain = GetDomain(strDomain);
if (strDomain == null) return "Domain is null or error!";
string result = "";
UTF8Encoding utf8 = new UTF8Encoding();
// Connect to the whois server
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(server, 43);
NetworkStream networkStream = tcpClient.GetStream();
// Send the domain name to the whois server
strDomain = strDomain.Replace("0x00", "");
byte[] buffer = Encoding.GetEncoding("GB2312").GetBytes(strDomain + "\r\n");
//byte[] buffer = utf8.GetBytes(domain + "\r\n");
networkStream.Write(buffer, 0, buffer.Length);
// Read back the results
buffer = new byte[10240];
int i = networkStream.Read(buffer, 0, buffer.Length);
while (i > 0)
{
i = networkStream.Read(buffer, 0, buffer.Length);
result += utf8.GetString(buffer);
//result +=Encoding.GetEncoding("GB2312").GetString(buffer);
}
networkStream.Close();
tcpClient.Close();
//return the lookup resutlt
result = result.Replace("\u0000", "");
return result;
}
public static string GetDomain(string strDomain)
{
if (String.IsNullOrEmpty(strDomain)) return null;
string domain = strDomain.Trim().ToLower();
if (domain.StartsWith("www."))
{
return domain.Substring(4, domain.Length - 4);
}
if (domain.IndexOf(".") < 0) domain += ".com";
if (domain == "" || domain.IndexOf(",") != -1 || domain.IndexOf(";") != -1 || domain.IndexOf("_") != -1 || domain.IndexOf(".") == -1)
{
return null;
}
return domain;
}
}
时间有限,不多写了,希望大家多给给意见
相关文章推荐
- 域名Whois信息查询(ASP.NET+C#)
- Whois查询ASP.NET C# 源码 查询域名信息
- ASP.NET域名查询系统源代码,域名Whois查询,(.Com/.Net/.Org/.Cn),c#语言实现域名Whois信息查询
- 域名Whois信息查询(ASP.NET+C#)
- 域名Whois信息查询(ASP.NET+C#)
- WHOIS查询检索,域名信息查询工具软件
- 《Advanced Bash-scripting Guide》学习(十):利用whois查询域名信息
- linux下查询域名或IP注册信息的操作记录(whois)
- Java正则表达式进阶(二):简单查询域名的Whois信息
- 『原创』再谈用 php 实现域名 whois 信息查询
- Asp.Net中查询域名的Whois信息
- Asp.Net中查询域名的Whois信息
- 如何通过whois查询域名所有人的信息?
- linux下查询域名或IP注册信息的操作记录(whois)
- 使用python-whois模块查询域名信息
- c# asp.net获取域名whois信息
- linux java调用whois命令查询域名信息
- C#下用WMI查询系统信息
- (python)域名查询服务(whois)程序+检查5位以内域名到期时间邮件报警抢注域名
- 分享C#域名查询代码!